LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Segmentation fault (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-247409/)

santhosh_o 10-26-2004 05:33 AM

Segmentation fault
 
hai everybody,
I worte a program and it does prints the output and after that it showed me segmentation fault error. could anybody help me out.

program is as follows:


#include <stdio.h>




int main()
{
int i,j;
FILE *f;
char *s1,*s2;


f=fopen("junk","w");

scanf("%s",s1);
if (!f)
return 1;
fwrite(&s1,sizeof(s1),1,f);
fclose(f);


f=fopen("junk","r");
if (!f)
return 1;
fread(&s2,sizeof(s1),1,f);
printf("%s\n",s2);

fclose(f);
printf("\n");

return 0;
}

itsme86 10-26-2004 05:38 AM

Quote:

char *s1,*s2;

scanf("%s",s1);
You can't do that. s1 needs to point to some reserved space in which to store the string. Right now scanf() is storing the input string into a random location in memory. You're lucky it's even getting past that.

You can either make it like char s1[50]; or keep your declaration the same and malloc() some memory.

santhosh_o 10-26-2004 05:42 AM

can u help me in this
 
Thank you , I will try that out.

can u help me in this.

Now I am trying to create own login screen and authentication. Actually my login screen should come first and after that only the gdm login screen should appear. I have created my login screen using gtk+. I have redhat linux 9(shrike) and kernel version is 2.4.20-6smp. My default runlevel is 5. I am trying this for past one week but i am not getting the my login screen first when linux boots up. Please help me in this.

santhosh_o 10-26-2004 05:45 AM

thankyou
 
can u help me in this ( post #)

Thank you , I will try that out.

can u help me in this.

Now I am trying to create own login screen and authentication. Actually my login screen should come first and after that only the gdm login screen should appear. I have created my login screen using gtk+. I have redhat linux 9(shrike) and kernel version is 2.4.20-6smp. My default runlevel is 5. I am trying this for past one week but i am not getting the my login screen first when linux boots up. Please help me in this.


All times are GMT -5. The time now is 12:28 PM.