The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
Please vote in the Forum Structure Poll. Polling will close at 2PM EST on January 21, 2025.
C and Segmentation Faults (strcmp/fclose)
Seguerof the VoidSydney, AustraliaRegistered Userregular
I have a C assignment (not going to get you guys to do it for me :P) that is causing me headaches; I'm using fgets to get lines from a file (which works fine), but if I try to strcmp the line that results from that, I get a segmentation fault. Unless I printf the line first. Then, upon trying to close the file using fclose I get another fault, even after adding an if statement to check that the file pointer is not null!
Isn't it safer to use strncmp instead of strcmp? It won't solve the problem, but it's good practice (prevents buffer overflows, which *may* be the problem) and it might at least prevent the segfault.
EDIT: As well, strncpy.
theSquid on
0
Seguerof the VoidSydney, AustraliaRegistered Userregular
edited November 2007
Alright well rather than start a new thread:
Does anyone know how to declare a pointer to a pointer to a struct, so that I can edit the values in the struct? I have plenty of line* start working, but how do I do that with line** start, etc?
Seguerof the VoidSydney, AustraliaRegistered Userregular
edited November 2007
I'll have to try it later, but how would I then edit the values in (**p)?
Seguer on
0
SmasherStarting to get dizzyRegistered Userregular
edited November 2007
I think there's some sort of misunderstanding going on. Why do you need a pointer to a pointer to a struct to change the values of the variables inside it?
There are times when pointers to pointers to things are appropriate (multidimensional arrays, for example), but in this case I suspect you're unintentionally overcomplicating things. What exactly are you attempting to do?
Posts
char line[MAX_CHARS_TO_READ]; //space of 100
fgets(line, MAX_CHARS_TO_READ, packets)
EDIT:
strcmp stuff:
char *messageID;
messageID = strtok(cp, ":");
if (strcmp(messageID, "END\n"))
{
return 1;
}
EDIT: SOLVED It was strcpy causing it. Argh.
EDIT: As well, strncpy.
Does anyone know how to declare a pointer to a pointer to a struct, so that I can edit the values in the struct? I have plenty of line* start working, but how do I do that with line** start, etc?
// assign stuff
(*p)->field = value;
// or (**p).field = value;
Did that help?
There are times when pointers to pointers to things are appropriate (multidimensional arrays, for example), but in this case I suspect you're unintentionally overcomplicating things. What exactly are you attempting to do?