簡體   English   中英

如何在 C 中返回指向結構數組的指針

[英]How to return pointer to array of struct in C

我使用malloc在 function 中創建了一個student結構,並用數據填充了它。 我想將結構數組的地址返回給main並逐行打印,但是在我的實現中,它沒有打印。 我調試了我的代碼,確實,它能夠將我的數組的地址返回給main 我不知道為什么它不打印。 有什么想法嗎?

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

typedef struct student Student;

struct student
{
    char name[100];
    int age;
    char sex;
};

Student **getstudents(int n)
{
    Student **t = malloc(sizeof *t * n); // memory for the array of pointers
    for (int i = 0; i < n; i++) // memory for each individual pointer
    {
        t[i] = malloc(sizeof **t);
    }
    /* Data is inputted by user with form <student> <sex> <age>, then get mapped to the struct *t */

    return t; /* Pointer to an array of pointers */
}


int main()
{
    Student **students;
    int n;
    scanf("%d\n", &n);
    students = getstudents(n);
    for (int i = 0; i < n; i++)
    {
        printf("Name: %s, Sex: %s, Age: %d\n", students[i]->name, students[i]->sex, students[i]->age);
    }

    for (int i = 0; i < n; i++)
    {
        free(students[i]);
    }
    free(students);
    return 0;
}


I am only allowed to modify the code in `Student **getstudents(int n)`. 

在行中:

Student *t = (char*)malloc(sizeof(Student)*n); 

您正在為一個指針分配 memory ,如果要返回指向指針的指針,則需要相應地分配 memory :

Student **t = malloc(sizeof *t * n); // memory for the array of pointers

for(int i = 0; i < n; i++){ // memory for each individual pointer
    t[i] = malloc(sizeof **t);
}

要稍后釋放指針,您還需要釋放先前分配的每個單獨的指針:

for(int i = 0; i < n; i++){ // memory for each individual pointer
    free(students[i]);
}
free(students);

請注意,單個字符的說明符是%c ,需要更正 printf :

printf("Name: %s, Sex: %c, Age: %d\n", students[i]->name, students[i]->sex, students[i]->age);
//                     ^^

我要更改的另一件事是在strncpy而不是 null 稍后終止字符串,我會讓 function 這樣做:

// one more byte and strncpy terminates the string for you
strncpy(t[i]->name, data[0], strlen(data[0]) + 1); 
//      ^^^^
// already corrected for the new pointer

更正了此處的問題是一種可能的替代方法,您可以使用sscanf從一個 go 中的entry解析結構中的所有元素,如果您願意:

Student **getstudents(int n)
{
    Student **t = malloc(sizeof *t * n); // memory for the array of pointers

    if (t == NULL)
    {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    for (int i = 0; i < n; i++) // memory for each individual pointer
    {
        t[i] = malloc(sizeof **t);
        if (t[i] == NULL)
        {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
    }

    for (int i = 0; i < n; i++)
    {
        char entry[100];
        if (fgets(entry, sizeof entry, stdin))
        {
            if (sscanf(entry, "%25s %c %d", t[i]->name, &t[i]->sex, &t[i]->age) != 3)
            {
                // deal with bad input
            }
        }
    }
    return t;
}

anastaciu 的回答指出了很多麻煩,但還有其他問題:

您的代碼中的另一個問題是您使用 '%s' 作為性別,因為性別是唯一的字符。 您應該使用 %c 否則 printf function 將嘗試解析字符串並獲得 SEGFAULT。

我也敦促您嚴格檢查每個 memory 分配。 總是。

我的pov修改后的代碼:

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

typedef struct student Student;

struct student{
    char name[100];
    int age;
    char sex;
};

Student **getstudents(int);
void free_students(Student**, int);

int main(){
    Student **students;
    int n = 4;
    students = getstudents(n);
    for(int i = 0; i < n; i++){
        if (students[i] != NULL) {
             printf("Name: %s, Sex: %c, Age: %d\n", students[i]->name, students[i]->sex, students[i]->age);
        }
    }
    free_students(students, n);
    return 0;
}

Student **getstudents(int n){
    Student **t = (Student **)malloc(sizeof(Student *)*n);
    if (t==NULL) {
       perror("Memory: can't allocate.");
       return(NULL);
    }

    /* Input: <name> <sex> <age> */
    char entry[100];
    for(int i = 0; i < n; i++){
        t[i]=NULL;
        if (fgets(entry,100,stdin) != NULL) {
            int readBytes = strlen(entry);
            char newString[3][25];

            int k,j,ctr;
            j=0; ctr=0;
            for(k=0;k<=readBytes;k++)
            {
                if(entry[k]==' '||entry[k]=='\0'||entry[k]=='\n')
                {
                    newString[ctr][j]='\0';
                    ctr++;
                    j=0;
                }
                else
                {
                    newString[ctr][j]=entry[k];
                    j++;
                }
            }

            t[i] = (Student *)malloc(sizeof(Student));
            if (t[i] == NULL) {
               perror("Memory: can't allocate.");
               return(NULL);
            }
            strncpy(t[i]->name, newString[0], strlen(newString[0]));
            t[i]->name[strlen(newString[0])] = '\0';
            t[i]->sex = *newString[1];
            t[i]->age = atoi(newString[2]);
       }
    }

    return t;
}

void free_students(Student **students, int n){
    for(int i=0; i<n; i++){
        free(students[i]);
    }
    free(students);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM