Thread: Sementation fault in GCC when using pointer
hi every body.i'm working on gcc writing program. encountered problem after ran program. mean it's compiled in run time gives me segmentation fault error. here code::
finally when i've changed definition of string of code below code works finephp code:
#include<stdio.h>
#include<string.h>
int main()
{
char *string="";
char *string1="";
scanf("%s",string);
scanf("%s",string1);
printf("%s",string);
printf("%s",string1);
return 0;
}
i wanna know can use pointer string or not????php code:
char string[1000];
char string1[1000];
machine runs in ubuntu 11.04 x64
you can use pointer have make point allocated memory. char *string="" makes point 1 byte ('\0'). have use:
but in c, pointers , arrays same concept. when declare array, variable holds array pointer first element of array, valid:code:#include <malloc.h> char *string; string=malloc(1000);
code:#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv){ char x1,x2; char *string1="abcdefgh"; x1=string1[5]; x2=*(string1+5); printf("%c =? %c\n",x1,x2); char string2[]="abcdefgh"; x1=string2[6]; x2=*(string2+6); printf("%c =? %c\n",x1,x2); return 0; }
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Sementation fault in GCC when using pointer
Ubuntu
Comments
Post a Comment