Home > Enterprise >  Why am I getting a segmentation fault (core dumped) from scanf?
Why am I getting a segmentation fault (core dumped) from scanf?

Time:12-08

I'm writing a program where we have to just ask the user for a movie title, adult tickets sold, and child tickets sold and then it displays calculated information based on what was entered. I'm getting a Segmentation fault (core dumped) error very early into the program. I'm wondering why I'm getting it and how to resolve?

Here is the beginning of the program. I get the error after entering a value for the number of adult tickets sold. I am able to input the movie name without error. I've read the error is because I am trying to reference something I don't have access to. I guess I'm just confused on the syntax and maybe if I am even declaring the variables correctly or referencing them properly in the scanf statements.

#include <stdio.h>
#include <string.h>

int main()
{
    //Defines constant variables
    const double adultPrice = 10, childPrice = 6;
    const double profitMargin = .2;

    //Defines variables for the number of tickets sold
    double adultTix, childTix, gross, adultGross, childGross, net, paidToDist;

    //Defines variable to hold name of movie
    char movieName[50];

    //Asks user for name of movie
    printf("Please enter the movie name: ");
    scanf("%s", movieName);

    //Asks user for # of adult tickets sold
    printf("Please enter the number of adult tickets sold: ");
    scanf("%f", adultTix);

    //Asks user for # of child tickets sold
    printf("Please enter the number of child tickets sold: ");
    scanf("%f", childTix);
}

CodePudding user response:

In order for scanf to know where to store the data it reads, you need to tell it two things:

  1. What datatype you are storing into;
  2. Where that data lives in memory.

When you do this:

scanf("%f", adultTix);  //<-- incorrect

You are saying that you want to read a float, and it should be stored in the memory location pointed to by the value of adultTix. The problem is that the value of adultTix is not actually a pointer (memory address), and furthermore it does not even have a defined value.

So, what happened is whatever value its place in memory contained when you called scanf was converted to a pointer value and then scanf wrote some data to that location in memory.

The correct way is to get the memory location of your variable using the reference operator &. This will return a pointer to that variable's data. e.g. &adultTix returns a pointer to (address of) the first byte of the double value referred to by the name adultTix. And you need to tell scanf that it's a double by using %lf as the format specifier.

So, your three scanf calls should look like this:

scanf("%s", movieName);
scanf("%lf", &adultTix);
scanf("%lf", &childTix);

Note above that when you're reading a string into an array (which is the case with movieName) you don't need to use &, because movieName will automatically be converted to a pointer when passed as a parameter. If you really want, you can be explicit with &movieName[0].

CodePudding user response:

Use of & in scanf()

The Correct Code will be

#include <stdio.h>
#include <string.h>

    int main()
    {
            //Defines constant variables
        const double adultPrice = 10, childPrice = 6;
        const double profitMargin = .2;
    
        //Defines variables for the number of tickets sold
        double adultTix, childTix, gross, adultGross, childGross, net, paidToDist;
    
        //Defines variable to hold name of movie
        char movieName[50];
    
        //Asks user for name of movie
        printf("Please enter the movie name: ");
        scanf("%s", &movieName);
    
        //Asks user for # of adult tickets sold
        printf("Please enter the number of adult tickets sold: ");
        scanf("%f", &adultTix);
    
        //Asks user for # of child tickets sold
        printf("Please enter the number of child tickets sold: ");
        scanf("%f", &childTix);
        }
  • Related