簡體   English   中英

訪問動態分配的結構指針數組的成員

[英]Accessing members of dynamically allocated array of pointers to structs

我需要一個全局動態指針數組,我將在其中存儲我的結構,因為稍后我需要遍歷此數組以列出所有存儲的信息,我還需要能夠讀取nameagejob來自控制台的變量,並將它們存儲在iterator數組中的person_t中。

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

typedef struct Person
{
    char name[30];
    int age;
    char job[30];
} person_t;

person_t **iterator;
int capacity = 10;
int size = 0;

int main()
{
    int i;
    *iterator = (person_t *)malloc(capacity * sizeof(person_t));
    for (i = 0; i < capacity; ++i)
    {
        person_t p;
        p.age = i;
        *iterator[i] = p;
    }
    return 0;
}

我在編譯此代碼 (gcc -ansi -pedantic -Wall -Wextra) 時沒有收到任何錯誤/警告,但是當我嘗試運行它時,我立即收到Segmentation fault

當你這樣做時:

*iterator = (person_t *)malloc(capacity * sizeof(person_t));

您正在取消引用iterator ,但是作為文件范圍指針變量,它被初始化為 NULL。嘗試取消引用 NULL 指針會調用未定義的行為

我懷疑您真正想要的是一個結構數組,而不是一個指向結構的指針數組。 既然如此,定義iterator為:

person_t *iterator;

然后像這樣為它分配 memory:

iterator = malloc(capacity * sizeof(person_t));

然后像這樣分配給數組元素:

iterator[i] = p;

您聲明的目的是創建一個“全局動態指針數組,我將在其中存儲我的結構” 對您的代碼進行以下修改(請參閱評論)將執行此操作:

person_t p[10] = {0};

int main()
{
    int i;
    // with declaration: person_t **iterator = NULL;, 
    //following is all that is needed to create an array of pointers:
    iterator = malloc(capacity * sizeof(person_t *));//no need to cast return of malloc
   
        
        for (i = 0; i < capacity; ++i)
        {
            //person_t p;//moved to scope that will exist outside of main()
            p[i].age = i;
            iterator[i] = &p[i];//assign the address of the object to the pointer
                             //iterator[i] is the ith pointer in a collection of 
                             //pointers to be assigned to point to 
                             //instances of struct person_t 
        }
        //Once all fields are populated (to-do), the following will display the results:
        for (i = 0; i < capacity; ++i)
        { 
           printf("%d) Name: %s Age: %d  Job: %s\n",  i, iterator[i]->name,iterator[i]->age,iterator[i]->job);
        }

    return 0;
}

你沒有正確分配 memory

首先,您需要為一個指針分配 memory,該指針可以存儲地址的capacity數,即通過iterator = malloc(capacity * sizeof(person_t*)); 然后你需要分配 memory 來保存每個結構元素,即iterator[i] = malloc(sizeof(person_t));

一旦我們完成了它,所有malloc和 memory 應該是free的。

此外,還沒有對malloc進行錯誤檢查,留給你作為練習。

int main()
{
    int i;
    // test data
    char *names[] = {"ABC", "DEF"};
    char *jobs[] = {"Accountant", "Security"};
    int ages[] = {50, 60};
    
    // first allocate memory for iterator , which can hold pointers to store iterator poniters
    iterator = malloc(capacity * sizeof(person_t*)); 
    
    for (i = 0; i < capacity; ++i)
    {
        // now allocate memory for individual iterator
        iterator[i] = malloc(sizeof(person_t)); 
        strcpy(iterator[i]->name,names[i]);
        iterator[i]->age = ages[i];
        strcpy(iterator[i]->job, jobs[i]);
    }
    
    for (i = 0; i < capacity; ++i)
    {
        printf("name = %s ", iterator[i]->name);
        printf("Age = %d ", iterator[i]->age);
        printf("Job = %s\n", iterator[i]->job);
    }
    return 0;
}

暫無
暫無

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

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