2

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);
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
birchwoody
  • 39
  • 4
  • 1
    You must pass a _pointer_ into `scanf`. _e.g._ `scanf("%f", &adultTix);` Please review the chapter on basic I/O in whatever book you're using to learn C. A decent compiler should even emit a warning when compiling this code, alerting you to the issue. – paddy Dec 08 '22 at 01:12
  • 1
    Does your compiler give you any warnings? If it does, you should fix those. – pmacfarlane Dec 08 '22 at 01:13
  • 3
    @SamVarshavchik In fairness, OP explained that at *end* of C++ course, they had an assignment to write some pure C. – Steve Summit Dec 08 '22 at 01:18
  • @birchwoody To explain what's going on here (i.e. paddy's comment): `scanf` needs the equivalent of reference parameters, but C doesn't have those, so you must explicitly pass pointers instead. – Steve Summit Dec 08 '22 at 01:19
  • So he assigned you to rewrite in C, but never actually taught C? Was it a prerequisite for the C++ class that you forgot about? – Barmar Dec 08 '22 at 01:20

2 Answers2

2

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].

paddy
  • 60,864
  • 6
  • 61
  • 103
1

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);
        }