0

EDIT: solved it, turns out I should use %c not %s because foodSelect and foodSize are characters not strings :P thanks

I'm trying to pass 3 values to the function output: foodChoice, foodSelect, foodSize (and foodOrderNum, and foodSubtotal but I haven't gotten around to that yet).

However, when I try to printf foodSelect, I get a segmentation fault, but when I try to print foodChoice, I don't. When I try to printf foodSize it just shows nothing.

source:

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

void question (char choice[]);
int output(char *foodChoice, char *foodSelect, char *foodSize);


void question (char choice[]) {

    char choiceYesNo;
    char *foodOptions;
    char *foodChoice;
    char *foodSelect;
    char *foodSize;
    int foodOrderNum = 0;
    float foodSubtotal = 0;

    switch (choice[0]) {
        case 'f':
            foodChoice = "Fish";
            foodOptions = "(K- Haddock, T- Halibut)";
            break;
        case 'c':
            foodChoice = "Chips";
            foodOptions = "(C- Cut, R- Ring)";
            break;
        case 'd':
            foodChoice = "Drinks";
            foodOptions = "(S- Softdrink, C- Coffee, T- Tea)";
            break;
    }

    printf("Do you order %s? (Y/N): ", foodChoice);
        scanf("%c", &choiceYesNo);
    printf("%s choice %s: ", foodChoice, foodOptions);
        scanf("%s", &foodSelect);
    printf("What size (L - Large, M - Medium, S - Small): ");
        scanf("%s", &foodSize);
    printf("How many orders do you want? (>=0): ");
        scanf("%d", &foodOrderNum);
    output(foodChoice, foodSelect, foodSize);
}

int output(char *foodChoice, char *foodSelect, char *foodSize) {

    // printf("You ordered %s: %s - SIZE: %s   amount ordered: , subtotal price: \n", 
    // foodChoice, foodSelect, foodSize);

    printf("\n\n%s\n", foodSelect);
    // printf("\n\n%s\n", foodSelect);

}

int main() {

    question("chips");


}
1
  • You don't need to edit your question to say it's solved! Just click on the checkbox near the answer that solved your question to select it as the official answer to the question. This is much more visible than editing the question. Commented Mar 26, 2012 at 19:54

2 Answers 2

2

You haven't allocated memory for:

char *foodOptions; char *foodChoice; char *foodSelect; char *foodSize;

Do malloc and allocate memory. Note that:

char *foodChoice="Fish";

And

char *foodChoice;
foodChoice="Fish";

are not the same.

Sign up to request clarification or add additional context in comments.

4 Comments

Not allowed to use malloc for we have not used it in class. Not sure why you used foodChoice as an example since that's the only one I have working right now haha.
Either malloc or just an array char foodChoice[MAX_EXPECTED_LENGTH]
I just used one of your variables to point out your error, that's it. The same applies to other pointer variables you used as well. If don't want to use malloc then declare them as @stanwise said.
On a second thought, there is no error here. You use these pointers just to reference static strings defined in the code, which is correct. The way you wrote it is not very readable though.
1

This is because you pass &foodSelect to scanf, which is incorrect for C strings. You should pass foodSelect instead, no ampersand.

You should also allocate sufficient space to store the values the users enter, and instruct scanf on the max size of the buffer.

#define MAX_BUF 128

...
char foodSelect[MAX_BUF];
...
scanf("%127s", foodSelect);

1 Comment

If the buffer size if 128, you should use %127s because of the null terminator. Otherwise you are vulnerable to an off-by-one overflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.