One of the most common errors in C programming, which happens during pointer exception. The program is trying to access memory that does not belongs to it. This is similar to Java's version of NullPointerException.
For example if we do this in C,
char *s = "hello world";
*s = 'H';
When the program containing this code is compiled, the string "hello world" is placed in the section of the program binary marked as read-only; when loaded, the operating system places it with other strings and constant data in a read-only segment of memory. When executed, a variable, s, is set to point to the string's location, and an attempt is made to write an H character through the variable into the memory, causing a segmentation fault. (from wiki)
I had a similar problem such as:
char *s = "123";
int num = (int) *s;
I solved this problem using atoi(); which converts the ASCII characters into int.
char *s = "123";
int num = atoi(*s);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment