ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i found error messages like this : Segmentation fault (Core dumped) when i compile it:"gcc LIFO.c" then run it:
"./a.out <sedikit.txt"
notes:
i use redhat 7.0
sedikit.txt is textfile, it contains:
abjad
anggur
baku
bento
laskar
tugas
tunas
i'd like to insert every text to be LIFO, but in the early i got problem like that???
AFA what the code is trying to do, the problem here is that you've created a pointer to a memory location (char *S), and are assigned data to it (fgets(S, 30, stdin)) before any actual heap space is assigned to the pointer (*S). In order to use the space at *S, you need to allocate it first. There are two ways to go about this:
Code:
// staticly define S as a string, with 30 characters of space
char S[30];
...
fgets(S,30,stdin);
or
Code:
// dynamically assigned memory space for input
char *S;
S = (char *)malloc(sizeof(char) * 30);
if(S == NULL)
perror("Unable to malloc space for S");
else {
fgets(S,30,stdin);
printf("%s", S);
}
One of those options should solve your segfault problem.
Oh, and anytime you post code here, please surround the code with [code] [/code] tags, so that it will look nice, and be easier to read (preserving indentations) as above.
(=
Last edited by TheLinuxDuck; 07-09-2003 at 09:02 AM.
oh i understand now..
when i re-login, this forum can not submit my reply..
thanks @e
next i'll send it by
Code:
and
, cause i'm sure i'll have many problems in programming..
i'm trying make Last In First Out link list to text file but when i test to compile it, it occured that error message.
my basic is pascal programming, so i found myself in trouble when learn gcc.. huff
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.