Reputation: 21
I am trying to create a simple "diary", where you add workers and then write them how many hours they have worked. But every time I get to the switch statement, I get a segmentation fault. I don't know what is causing it and my knowledge is not yet on the level where I can find it myself. The code:
include <stdio.h>
include <stdlib.h>
include <string.h>
int vybera(int pocet){
int vyber;
int i;
printf("\nKtery zamestnanec?\n");
for(i = 0; i < pocet; i++){
printf("%d\n", i + 1);
}
scanf("%d", vyber);
return vyber;
}
int main(int argc, char** argv) {
int worker;
int pocet;
int i;
int vyber;
int pomoc;
int odprac = 0;
printf("Kolik zamestnancu chcete pridat: ");
scanf("%d%*c", &pocet);
char workforce[pocet][100];
printf("Zadejte %d jmen: \n", pocet);
for(i = 0; i < pocet; i++){
gets(workforce[i]);
}
for(i = 0; i < pocet; i++){
printf("%s\n", workforce[i]);
}
vyber = vybera(pocet);
switch(vyber){
case 1:
worker = vyber - 1;
printf("Vybrali jste %s.\n", workforce[worker]);
printf("Pocet odprac. hodin: ");
scanf("%d", pomoc);
odprac += pomoc;
strcat(workforce[worker], (", %c", odprac + '0'));
break;
case 2:
worker = vyber - 1;
printf("Vybrali jste %s.\n", workforce[worker]);
printf("Pocet odprac. hodin: ");
scanf("%d", pomoc);
odprac += pomoc;
strcat(workforce[worker], (", %c", odprac + '0'));
break;
case 3:
worker = vyber - 1;
printf("Vybrali jste %s.\n", workforce[worker]);
printf("Pocet odprac. hodin: ");
scanf("%d", pomoc);
odprac += pomoc;
strcat(workforce[worker], (", %c", odprac + '0'));
break;
case 4:
worker = vyber - 1;
printf("Vybrali jste %s.\n", workforce[worker]);
printf("Pocet odprac. hodin: ");
scanf("%d", pomoc);
odprac += pomoc;
strcat(workforce[worker], (", %c", odprac + '0'));
break;
case 5:
worker = vyber - 1;
printf("Vybrali jste %s.\n", workforce[worker]);
printf("Pocet odprac. hodin: ");
scanf("%d", pomoc);
odprac += pomoc;
strcat(workforce[worker], (", %c", odprac + '0'));
break;
default:
printf("Spatny vyber.\n");
}
for(i = 0; i < pocet; i++){
printf("%s\n", workforce[i]);
}
return (EXIT_SUCCESS);
}
I am really at my wits end, and need every help I can get. The reason why I'm posting this even though there are many question on the segementation fault topic is that none of them are using my code. I appreciate any help and all of your patience. EDIT: I'm from Czechia, hence why most of my variables and all my printf outputs are in czech. Just FYI.
Upvotes: 2
Views: 611
Reputation: 2781
scanf("%d", pomoc);
This is incorrect syntax for the scanf()
function. It should be:
scanf("%d", &pomoc);
This same error is replicated in the input statement for vyber
scanf("%d", &vyber)
This happens because you are trying to access an out-of bounds location in memory with the scanf()
function. Since you don't reference the variable vyber
and pomoc
, the compiler tries to access the memory spot at an address equal to the variable stored in vyber
and pomoc
, rather than the memory location of these respective variables. This goes into undefined behavior, which is why you get a seg-fault.
Upvotes: 2
Reputation: 50378
You've already been told where the problem is, but let me briefly mention how you can find the (next) problem yourself. Basically, there are (at least) three ways:
Use a debugger to run your program line by line until it crashes. A debugger will also let you stop the program and examine the values of any variables to see what the code is actually doing.
You haven't told us what OS, compiler and IDE (if any) you're using, so I can't recommend any specific debugger (and in any case that's really not on-topic for SO), but there almost certainly is a debugger available for your system, whatever it is. Learning how to use a debugger takes a little effort, but it's definitely worth it.
If you can't or don't want to use a debugger for some reason, you can do "poor man's debugging" by adding printf()
statements into your code that show what's happening. That way, you can see how far the program gets before crashing, and you can also print out the values of variables to get a better idea of what's happening.
I would recommend printing to the standard error stream (with fprintf(stderr, "...")
), since it won't interfere with normal output of the program, and since the error stream will be automatically flushed every time you print to it, so that there's no risk of buffered output being lost if the program crashes.
A useful trick is to prefix such debugging print statements with if (DEBUG)
, like this:
if (DEBUG) fprintf(stderr, "This is an example of debugging output.\n");
Then, at the top of your program, include a line like:
#define DEBUG 1
That way, you can easily turn the debugging output off by changing the 1
to a 0
. (There are also more advanced ways to change the definition at compile time, or to configure your IDE to change it depending on the build target, but that's outside the scope of the answer.)
Finally, you can do "bisection debugging". Basically, make a copy of your program (or, better yet, commit it into your version control system) and start removing parts of the code until the problem stops happening. At that point, you'll know that the problem is related to something in the last piece of code you removed. You can then undo the most recent change, and remove something else, and keep doing that until you've reduced your program to the simplest possible example that still demonstrates the problem.
At that point, it should be easy to see what the problem is, since you no longer have any irrelevant code left to distract you. Or, if not, at least you have a short piece of code that cleanly demonstrates the problem and which you can post on Stack Overflow to ask for help. :)
Upvotes: 2
Reputation: 2065
change: scanf("%d", vyber); to scanf("%d", &vyber) do the same with pomoc;
Explanation: Because vyber variable is int you need to explicitly pass the address value with "&" ampersand, unlike a string(which implicitly passes its address). The segmentation fault indicates you are trying to access a memory which doesn't belong to you. In this case vyber.
Upvotes: 2