Programming help : STUPID C! HELP! :)

bsobel

Moderator Emeritus<br>Elite Member
Dec 9, 2001
13,346
0
0
Is this your actual code, as I don't see how it can compile. In main you reference input and input2 and then go and use input and input1.
Bill
 

trilobyte

Member
Feb 27, 2000
60
0
0
Sorry, typo when I was putting it in

I just found out something odd

in my main i have this:
printf("%s\n", books[0]->author.last);
printf("%s\n", books[1]->author.last);

which gives me a segmentation error on the second statement
BUT, if I only have this:
printf("%s\n", books[1]->author.last);

I'm able to acess the last name correctly.

Any ideas as to what causes it to crash when I access books[0] then access books[1]?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
For God's sake, PLEASE host the code somewhere and give us a link! I refuse to look at unformatted code ;)
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Is the code as-given (including the main procedure) supposed to give Segmentation Errors? The code runs fine for me. Maybe your data file is different (post it too ;))?

By the way, is this for an assignment, or some other reason why you're writing it in C?
 

trilobyte

Member
Feb 27, 2000
60
0
0
here are the data files:

test.dat
test2.dat

It worked for you? Lemme see what you were using for data. What are you running it in?

Yeah, it's for my C class. I'm not cheating ;) I'm just debugging.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: trilobyte
here are the data files:

test.dat
test2.dat

It worked for you? Lemme see what you were using for data. What are you running it in?

Yeah, it's for my C class. I'm not cheating ;) I'm just debugging.

I just tested the program with your files. Works fine. I am testing with MSVC++ on Windows 2000. Let me try to test on a Linux machine.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Just tested on a Linux machine with gcc. No problems. Are you sure the program doesn't work? :confused:
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: trilobyte
Ug?? HOW THE HELL! I get segmentation errors :/

can you paste your makefile?

No makefile. Here's the run output:

student% gcc book.c bookmain.c
student% a.out*
Enter a file name: x.txt
Enter 'r' for input or 'w' for output : r
Enter a file name: y.txt
Enter 'r' for input or 'w' for output : r
TWAIN
Bayler

 

trilobyte

Member
Feb 27, 2000
60
0
0
Hrm, my linux box doesn't seem to output any a.out from gcc, nor does the unix system at school. lemme try cygwin
 

trilobyte

Member
Feb 27, 2000
60
0
0
NM the post just above, was typing it in wrong.

anyway, i'm still getting "Segmentation Fault (core dumped)" :/ that's too weird how yours works though
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: trilobyte
Hrm, my linux box doesn't seem to output any a.out from gcc, nor does the unix system at school. lemme try cygwin

Its just the default executable name; it could be anything.
 

ynotravid

Senior member
Jun 20, 2002
754
0
0
Try using &nbsp ; to format your code.

It will allow you to use leading spaces like this:

while()
{
do this
then this
etc...
}

Or rather, it will insert spaces where you want them.
 

m0ti

Senior member
Jul 6, 2001
975
0
0
well, here's at least one of your problems:

it's a fairly common mistake that's made by programmers in C (especially when going back from C++/Java):

typedef char* string


...

string first, last;

what this translates to is:

char* first, last;

which in C is:

char* first;
char last;

The * is ALWAYS only applicable to the first one.

And that's why you can't access the last name. You're trying to set a charcter up as a pointer, something which will give you dubious results.

Set it up as:

string first;
string last;

And this problem may well disappear.


Good luck!

 

trilobyte

Member
Feb 27, 2000
60
0
0
Woohoo! It's working! No more segment errors!!

Thanks a whole bunch to everyone for trying to help. It was my malloc statement in my makeNode function. What happened was I got mixed up with my typedefs and should have done sizeof(bookType) instead of bookPtr (which was typed def'ed to * bookType). Anyway, *whew* it's working like a charm now.

Thanks again!
 

VirtualLarry

No Lifer
Aug 25, 2001
56,587
10,225
126
Originally posted by: m0ti
well, here's at least one of your problems:

it's a fairly common mistake that's made by programmers in C (especially when going back from C++/Java):

typedef char* string


...

string first, last;

what this translates to is:

char* first, last;

which in C is:

char* first;
char last;

The * is ALWAYS only applicable to the first one.

Might want to double-check your language reference manual. :)
What you wrote might be correct in terms of a define pre-processor macro, as those are literal replacements, but not with a typedef. Typedefs are semantic replacements.

So in fact it does translate to:

char* first, char* last;

That is not the cause of the error in the program.