| | |
A whole bunch of Segmentation Faults
![]() |
•
•
Join Date: Nov 2009
Posts: 8
Reputation:
Solved Threads: 0
Hi there,
This is my first time working with an unsafe language, so naturally my first code would be full of memory-related bugs. I still have no idea how I ought to approach some of the problems I've been getting, and I've been resisting asking for help as much as I could.
The assignment I've been given is a buttload of work, so I'd rather not bog anyone down with too much of it. At the moment, I'm trying to work out how to design a menu() function that gives main() both the function choice the user wants and the arguments for that function. Here's what I've got so far:
This code is saved as menu.c, which is included in the inclusion statements of the main function. I'm getting a segmentation fault at and I haven't been able to find out why. I also get the sense main won't be able to access reply for some reason.
I just need a push, and it's probably so simple it went over my head, but can someone help me out?
This is my first time working with an unsafe language, so naturally my first code would be full of memory-related bugs. I still have no idea how I ought to approach some of the problems I've been getting, and I've been resisting asking for help as much as I could.
The assignment I've been given is a buttload of work, so I'd rather not bog anyone down with too much of it. At the moment, I'm trying to work out how to design a menu() function that gives main() both the function choice the user wants and the arguments for that function. Here's what I've got so far:
c Syntax (Toggle Plain Text)
struct REPLY { char use[256]; char pass[256]; char type[64]; char nuse[256]; char npass[256]; char ntype[64]; int option; }; struct REPLY *reply; int menu (void) { int o; printf("Please type a number corresponding to the following options:\n"); printf("1. Add\n"); printf("2. Delete\n"); printf("3. Edit\n"); printf("4. Purge\n"); printf("5. Quit\n"); scanf("Selection: %i\n", o); reply->option=o; printf("\n\n%i\n\n\n", reply->option); switch(reply->option){ case '1': { printf("Please provide the username.\n"); scanf("%s\n", &reply->use); printf("Please provide the password.\n"); scanf("%s\n", &reply->pass); printf("Please provide the user type.\n"); scanf("%s\n", &reply->type); break; } case '2': { printf("Please provide the username.\n"); scanf("%s\n",&reply->use); break; } case '3': { printf("Please provide the username.\n"); scanf("%s\n",&reply->use); printf("Please provide the password.\n"); scanf("%s\n",&reply->use); printf("Please provide the new username.\n"); scanf("%s\n",&reply->nuse); printf("Please provide the new password.\n"); scanf("%s\n",&reply->npass); printf("Please provide the new user type.\n"); scanf("%s\n",&reply->ntype); break; } case '4': { break; } case '5': { break; } } return 0; }
This code is saved as menu.c, which is included in the inclusion statements of the main function. I'm getting a segmentation fault at
c Syntax (Toggle Plain Text)
reply->option=o;
I just need a push, and it's probably so simple it went over my head, but can someone help me out?
-7
#2 19 Days Ago
line 22: >> scanf("Selection: %i\n", o);
You can't combine scanf() and printf() like that, and the parameter must be a pointer to an integer
You can't combine scanf() and printf() like that, and the parameter must be a pointer to an integer
C Syntax (Toggle Plain Text)
printf("Selction: "); scanf("%i", &o);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Nov 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#3 19 Days Ago
That dismissed one of the warnings, but I still get a segmentation fault at Why is that even a problem?
All of the other warnings look like this:
C Syntax (Toggle Plain Text)
reply->option=o;
All of the other warnings look like this:
C Syntax (Toggle Plain Text)
menu.c:30: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[256]’
2
#4 19 Days Ago
•
•
•
•
That dismissed one of the warnings, but I still get a segmentation fault atWhy is that even a problem?C Syntax (Toggle Plain Text)
reply->option=o;
struct REPLY *reply; is a pointer, anything you try to write to it it will produce a segmentation fault, since there's no memory allocated for it. 2
#6 19 Days Ago
•
•
•
•
I'm never sure when to use malloc and when C allocates the memory for me.
•
•
•
•
All of the other warnings look like this:C Syntax (Toggle Plain Text)
menu.c:30: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[256]’
scanf("%s\n", &reply->pass);•
•
Join Date: Nov 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#7 19 Days Ago
Success! This was the sort of push I needed. I'm not getting any warnings, and as far as I can tell the menu is working properly.
I might have questions later about how to get menu()'s reply back to main() since C's scoping is so complex. This is what I've done:
Since reply is declared globally, will main() be able to see and use it after menu is finished?
@Aia, thanks for your help - I really appreciate it.
I might have questions later about how to get menu()'s reply back to main() since C's scoping is so complex. This is what I've done:
c Syntax (Toggle Plain Text)
... struct REPL *reply; int menu (void) { reply=(struct REPL*)malloc(sizeof(struct REPL)); ...
Since reply is declared globally, will main() be able to see and use it after menu is finished?
@Aia, thanks for your help - I really appreciate it.
•
•
Join Date: Nov 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#8 19 Days Ago
Alright, more segmentation faults...
This time I'm trying to parse a file into a linked list. The intent is to decrypt and read the file line-by-line, separating a line into parts and storing those parts into a struct.
I'm only getting the segmentation fault when the file, password.csv, already exists, for some reason. If it's starting anew the program behaves normally.
GDB says the problem is in this line. The whole function is pasted below.
It happens in this function:
I'd really appreciate your thoughts.
This time I'm trying to parse a file into a linked list. The intent is to decrypt and read the file line-by-line, separating a line into parts and storing those parts into a struct.
I'm only getting the segmentation fault when the file, password.csv, already exists, for some reason. If it's starting anew the program behaves normally.
GDB says the problem is in this line. The whole function is pasted below.
C Syntax (Toggle Plain Text)
while(fgets(c, 600, fin)!=NULL)
It happens in this function:
c Syntax (Toggle Plain Text)
int parse (void){ cipher(0); FILE *fin=fopen("password.csv", "rt"); //processing char *c; while(fgets(c, 600, fin)!=NULL){ char use[256]; char pass[256]; char type[64]; strcpy(use, strtok(c,", ")); strcpy(pass, strtok(NULL,", ")); strcpy(type, strtok(NULL,", ")); push(use, pass, type); } fclose(fin); return 1; }
I'd really appreciate your thoughts.
![]() |
Similar Threads
- mysterious segmentation fault (C)
- glutMouseFunc and Segmentation Faults (Game Development)
- Segmentation Fault - Need help correcting (C++)
- Big files bring segmentation faults (C)
- string input segmentation fault on linux (C++)
- Segmentation Fault (C)
- Need help with string manipulation and seg faults (C)
- yet another segmentation fault (C++)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C Forum
- Previous Thread: Error while declaring a structure
- Next Thread: delete same array element in C
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






