簡體   English   中英

帶有指針的 c 中的段錯誤

[英]seg fault in c with pointers

我有兩個功能可以提示用戶打印披薩的配料名稱並將其打印出來。 但是每當我試圖打印出可用的成分時,我都會遇到段錯誤。 而當我提示輸入成分時,如果我說我們今天有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;
    }

這是 output

您的get_ingredients問題有很多問題。

  1. 我注意到的第一個問題是您更改了參數ingredients的指針,將其更改為設置為 NULL 的指針get_ingredients 所以從現在開始從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. 現在第二個問題更多的是優化。 您設置了一個變量,然后在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. 應該分配一個指向char的指針。
ingredients_array = (char**)calloc(temp, sizeof(char*));

變化

ingredients_array = (char**)calloc(temp, sizeof(char**));
  1. while 循環可以替換為 for 循環。 對於這種情況,哪種循環更合適。
        
        //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++;
        }

改用 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 早些時候丟失了它的原始指針,所以這沒有用。
//This cannot be used because now points to ingredients_array and not to num_ingredients
*num_ingredients = temp;

現在是所有這些更改的代碼。

#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

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