简体   繁体   English

带有指针的 c 中的段错误

[英]seg fault in c with pointers

I have two fucntions that prompt the user to print ingredients names for pizza and print them out.我有两个功能可以提示用户打印披萨的配料名称并将其打印出来。 But whenever I'm trying to print out the available ingredients, I get a seg fault.但是每当我试图打印出可用的成分时,我都会遇到段错误。 And when I prompt to enter ingredients, if I say we have 3 available ingredients today, I can only type 2 in (as shown in the output picture)而当我提示输入成分时,如果我说我们今天有3种可用成分,我只能输入2(如output图片所示)

    int get_ingredients(char** ingredients,int* num_ingredients) {
        char **ingredients_array = NULL; /*this is an array of pointers points to single ingredient in get_item*/
        int temp,i,j;
        ingredients = &ingredients_array; /*ingredients is a pointer points to the array of pointers*/
        temp = *num_ingredients;
        printf("How many available pizza ingredients do we have today? ");
        scanf("%d",&temp);
        ingredients_array = (char**)calloc(temp, sizeof(char*));
        i = 0;
        printf("Enter the ingredients one to a line: \n");
        while (i < temp){
            *(ingredients_array+i) = get_item();
            i++;
        }
        i = 0;      
        printf("Available ingredients today are: \n");
        while(i < temp) {
            j = i+1;
            printf("%d",j);
            print(". ");
            printf("%s", **(ingredients_array+i));
            i++;
        }
        *num_ingredients = temp;
        return EXIT_SUCCESS;
    }
    char* get_item(){
        int i,a;
        char *each_ingredient= (char*)malloc(61*sizeof(char)); /*points to each input*/
        a = getchar();
        i = 0;
        while (a != EOF && (char)a != '\n'){
            *(each_ingredient+i)= (char)a;
            a = getchar();
            i++;
        }
        *(each_ingredient+i) = '\n';
        return each_ingredient;
    }

This is the output这是 output

There are many issues with your get_ingredients question.您的get_ingredients问题有很多问题。

  1. The first issue that I noticed is that you changed the pointer of the parameter ingredients , you changed it to the pointer get_ingredients which was set to NULL.我注意到的第一个问题是您更改了参数ingredients的指针,将其更改为设置为 NULL 的指针get_ingredients So accessing the data from ingredients pointer from now on will get you a segmentation fault , for trying to access an illegal address.所以从现在开始从ingredients指针访问数据会给你一个segmentation fault ,因为试图访问一个非法地址。
int get_ingredients(char** ingredients,int* num_ingredients) {
        char **ingredients_array = NULL; /*this is an array of pointers points to single ingredient in get_item*/
        int temp,i,j;
        
        //ingredients pointer is lost forever and set to the address of the pointer of ingredients_array
        ingredients = &ingredients_array; /*ingredients is a pointer points to the array of pointers*/
  1. Now the second issue is more of an optimization thing.现在第二个问题更多的是优化。 You set a variable, and then you changed it on the scanf , making the useless to set it to some value initially.您设置了一个变量,然后在scanf上对其进行了更改,这使得最初将其设置为某个值是无用的。
        //Temp is set to num_ingredients, but this is of no use,becuase this value is never used and is overwritten by scanf("%d",&temp);
        temp = *num_ingredients;
        
        printf("How many available pizza ingredients do we have today? ");
        scanf("%d",&temp);
  1. Should allocate pointer of a pointer to char.应该分配一个指向char的指针。
ingredients_array = (char**)calloc(temp, sizeof(char*));

Changes变化

ingredients_array = (char**)calloc(temp, sizeof(char**));
  1. The while loops can be replaced with a for a loop. while 循环可以替换为 for 循环。 Which a more appropriate type of loop for this case.对于这种情况,哪种循环更合适。
        
        //This can write it better with a for loop
        i = 0;
        printf("Enter the ingredients one to a line: \n");
        while (i < temp){
            *(ingredients_array+i) = get_item();
            i++;
        }
        i = 0;      

        printf("Available ingredients today are: \n");
        //Use for loop intead, because it more appropiate for this case
        while(i < temp) {
            //better to use i+1 instead of j, simply things
            j = i+1;
            //These two printf can be on the same line
            printf("%d",j);
            printf(". ");
            
            //Allocation error?
            printf("%s", **(ingredients_array+i));
            i++;
        }

Using a for loop instead, getting user input from standard function, and using less confusing indexing.改用 for 循环,从标准 function 获取用户输入,并使用较少混淆的索引。

printf("Enter the ingredients one to a line: \n");
//Using for loop
for(int i = 0;i < temp;i++)
{
    char ptr[80]; //Store user input
    scanf("%s", ptr); //Get user input
    ingredients_array[i] = strdup(ptr); //strdup is to make a another string with the same contents
}
        
printf("Available ingredients today are: \n");
for(int i = 0; i < temp;i++)
{
    //These two printf can be on the same line
    printf("%d",i+1);
    print(". ");
            
    //Allocation error?
    printf("%s\n", ingredients_array[i]);
}
  1. *num_ingredients lost its original pointer earlier, so this serves no use. *num_ingredients 早些时候丢失了它的原始指针,所以这没有用。
//This cannot be used because now points to ingredients_array and not to num_ingredients
*num_ingredients = temp;

Now the code with all those changes.现在是所有这些更改的代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
    
int get_ingredients(char** ingredients,int* num_ingredients) {
        char **ingredients_array = NULL; /*this is an array of pointers points to single ingredient in get_item*/
        int temp;
        
        
        printf("How many available pizza ingredients do we have today? ");
        scanf("%d",&temp);

        
        
        ingredients_array = (char**)calloc(temp, sizeof(char**));
        
        printf("Enter the ingredients one to a line: \n");
        //Using for loop
        for(int i = 0;i < temp;i++)
        {
            char ptr[80]; //Store user input
            scanf("%s", ptr); //Get user input
            ingredients_array[i] = strdup(ptr); //strdup is to make a another string with the same contents
        }
        
        printf("Available ingredients today are: \n");
        for(int i = 0; i < temp;i++)
        {
            printf("%d. ",i+1);   
            printf("%s\n", ingredients_array[i]);
        }
        *num_ingredients = temp;
        
        return EXIT_SUCCESS;
    }
    
    
    
int main()
{
    char** ingredients;
    int a;
    int foo = get_ingredients(ingredients, &a);
    return 0;
}

Output Output

How many available pizza ingredients do we have today? 4

Enter the ingredients one to a line: 
meat
MEAT
mEaT
no_salas
Available ingredients today are: 
1. meat
2. MEAT
3. mEaT
4. no_salad

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

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