|
character arrays; tricky C language pointer problem!
I have this very simple piece of C code.
--------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char x[]="hello";
char *xx[] = {"hello","there"};
int main()
{
printf("%c\n",xx[0][0]);
x[0]='a'; /* this can be done */
xx[0][0]='x'; /* this fails!!! segfault will occur, but WHY */
return(0);
}
---------------------------------------
Compile with gcc and run it.
As you can see, it will segfault. You can change a value in the character value of variable x, but not in xx.
I don't get that!?
What could I do to be able to change values in the initialized variable xx.
Thanks!
Last edited by drgilberto; 02-11-2006 at 11:06 PM.
|