简体   繁体   English

使用字符串,指针和函数在C中获取分段错误

[英]Getting a segmentation fault in C with usage of strings, pointers, and functions

EDIT: solved it, turns out I should use %c not %s because foodSelect and foodSize are characters not strings :P thanks 编辑:解决了它,原来我应该使用 %c 而不是 %s 因为foodSelect和foodSize是不是字符串的字符:P谢谢

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). 我试图将3个值传递给函数output :foodChoice,foodSelect,foodSize(以及foodOrderNum和foodSubtotal,但我还没有解决这个问题)。

However, when I try to printf foodSelec t, I get a segmentation fault, but when I try to print foodChoice , I don't. 但是,当我尝试foodSelec t时,出现了段错误,但是当我尝试打印foodChoice ,却没有。 When I try to printf foodSize it just shows nothing. 当我尝试printf foodSize它什么也没显示。

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


}

You haven't allocated memory for: 您尚未为以下项分配内存:

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

Do malloc and allocate memory. 执行malloc并分配内存。 Note that: 注意:

char *foodChoice="Fish";

And

char *foodChoice;
foodChoice="Fish";

are not the same. 不一样。

This is because you pass &foodSelect to scanf , which is incorrect for C strings. 这是因为您将&foodSelect传递给scanf ,这对于C字符串是不正确的。 You should pass foodSelect instead, no ampersand. 您应该通过foodSelect代替,不要使用&号。

You should also allocate sufficient space to store the values the users enter, and instruct scanf on the max size of the buffer. 您还应该分配足够的空间来存储用户输入的值,并指示scanf有关缓冲区的最大大小。

#define MAX_BUF 128

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM