Skip to main content

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::
php 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;

finally when i've changed definition of string of code below code works fine
php code:
char string[1000];
char string1[1000]; 
i wanna know can use pointer string or not????
machine runs in ubuntu 11.04 x64

quote posted k_mp view post
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::
php 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;

finally when i've changed definition of string of code below code works fine
php code:
char string[1000];
char string1[1000]; 
i wanna know can use pointer string or not????
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:
code:
#include <malloc.h>  char *string; string=malloc(1000);
but in c, pointers , arrays same concept. when declare array, variable holds array pointer first element of array, valid:
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

Popular posts from this blog

Falang and too many redirects - Joomla! Forum - community, help and support

Infinite loop detected in JErrorInfinite loop detected in JError - Joomla! Forum - community, help and support

logged out from joomla! - Joomla! Forum - community, help and support