char pointer problem
This is a discussion on char pointer problem within the C Programming forums, part of the General Programming Boards category; Hello
Why will this lead to crash:
Code:
char *tmp = "(some)(stupid)(string)
";
char *p = tmp;
int length = strlen(tmp);
...
-
char pointer problem
Hello
Why will this lead to crash:
Code:
char *tmp = "(some)(stupid)(string)\0";
char *p = tmp;
int length = strlen(tmp);
if ( (p[0] == '(') && (p[length-1] == ')') ) {
p[length-1] = '\0';
p++;
length--;
printf("%s \n", p);
} I just want to remove parenthesis at the begging and end..
Thanks for help
-
uint64_t...think positive
Code:
char *tmp = "(some)(stupid)(string)\0";
The compiler places that string on non-modifyable memory space, but it is legal to have a pointer to it. It is a constant string. If you need to modify it, acquire a copy first.
Code:
...
goto johny_walker_red_label;
johny_walker_blue_label: exit(-149$);
johny_walker_red_label : exit( -22$); A typical example of ...cheap programming practices.
-
Deathray Engineer
If you want a copy, then make an array:
Code:
char tmp[] = "(some)(stupid)(string)\0";
That will make tmp an array that starts out with an implicit copy of the string from its read-only location.
Also, string literals don't need to have the '\0' char appended at the end. If the array is big enough to hold the array, the '\0' will be there in the array. By not specifying the size, the array is made large enough to hold the entire array + the '\0'. So this is all you really need:
Code:
char tmp[] = "(some)(stupid)(string)";
Popular pages Recent additions
Similar Threads
-
By hicpics in forum C Programming
Replies: 8
Last Post: 12-02-2005, 11:36 AM
-
By InvariantLoop in forum C Programming
Replies: 13
Last Post: 02-04-2005, 08:32 AM
-
By darfader in forum C Programming
Replies: 9
Last Post: 08-22-2003, 08:21 AM
-
By OmniMirror in forum C Programming
Replies: 4
Last Post: 05-14-2003, 09:40 PM
-
By Unregistered in forum C Programming
Replies: 10
Last Post: 04-24-2002, 11:49 PM