Taylor Ash
Taylor Ash

Reputation: 3

Segmentation Fault - How do I figure out what is wrong with my code? - C

I'm extremely new to programming and am trying to work out a program. However, I'm not getting any useful errors. The program compiles, but I get a "segmentation fault" message halfway through execution. In this program I'm asking for a month and day as input,then call a function (my plan is to pass the date string, and the address of a tempmonth array and a tempday integer) that separates the date into the month and the day and stores them in the address. Then print the data in main. I've been trying for hours to figure out what I'm doing wrong with the pointers and addresses...but I just can't figure it out. Here's the entire code:

#include <stdio.h>
void separate(char*,char*,int*);

int main()

{    

        char date[12];
        printf("Enter a month and a day: ");
        fgets(date,12,stdin);

        char tempmonth[10];
        int tempday;
        separate(date,&*tempmonth,&tempday);

        printf("month is %s and day is %d",tempmonth,tempday);

        return 0;
}

void separate(char*date,char*tempmonth, int*tempday)

{

        sscanf(date,"%s %d",*tempmonth,*tempday);
}

(For some reason the * won't print by chardate and chartempmonth - it's supposed to be there.) Thank you in advance :)

Upvotes: 0

Views: 253

Answers (3)

Rajeev Singh
Rajeev Singh

Reputation: 3332

The compiler might have shown you warnings like this:

‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘int’

‘%d’ expects argument of type ‘int*’, but argument 4 has type ‘int’


As sscanf takes a pointer as an argument where you have passed a int that is why its creating a Segmentation fault (core dumped). You are accessing an address *tempmonth (the value stored in tempmonth), which might be inaccessible by your program. Inaccessible means that the address doesn't belong to the memory provided to your program by the OS or the process doesn't have required permissions to access it.

Change the line

sscanf(date,"%s %d",*tempmonth,*tempday);

to

sscanf(date,"%s %d",tempmonth,tempday);

as tempmonth and tempday are already pointers.

Also in calling separate(date,&*tempmonth,&tempday); writing &*tempmonth is equvalent to tempmonth because you are referencing and dereferencing the same pointer; both will cancel out.

Upvotes: 7

Baroudi Safwen
Baroudi Safwen

Reputation: 852

You should narrow the problem: what line is causing the segmentation fault?
To answer this, use experience to guess or you could use a debugger like gdb, you can insert a break point(s) in a line, when the program runs and reaches this line it will stop and you will have control over execution, you will be able to execute your program step by step.
In your example, try inserting a breakpoint in your function separate.
Now you will see that the Segmentation Fault happens when your os executes sscanf, search for the prototype of sscanf, compare the type of parameters you are passing with what sscanf is expecting and you will solve your problem.
`

Upvotes: 0

Puck
Puck

Reputation: 2120

separate(date,&*tempmonth,&tempday);

&*tempmonth is the address of the object pointed by tempmonth... which is tempmonth, so &*tempmonth --> tempmonth

sscanf(date,"%s %d",*tempmonth,*tempday);

sscanf needs addresses of where to store the data. As tempmonth is a pointer to the first character of your chain, *tempmonth IS the first character of your chain. Same with tempday, it is already the address, meaning that *tempday is the actual value. Therefore change this line to:

sscanf(date,"%s %d",tempmonth, tempday);

Upvotes: 1

Related Questions