简体   繁体   English

如何在C中声明指向struct的指针数组,然后使用它?

[英]How do declare array of pointer to struct in C and then use it?

My aim is do declare an array of pointer to my person struct and then use it to modify the name and the age of the persons.I am pretty lost in my code so any help would be appreciated.Thanks!!! 我的目的是声明一个指向我的人结构的指针数组,然后使用它来修改人的名字和年龄。我在代码中很迷失,因此将不胜感激。谢谢!

#include <stdio.h>

#define HOW_MANY 7

char *names[HOW_MANY]= {"p1", "p2", "p3", "p4", "p5", "p6", "p7"};

int ages[HOW_MANY]= {2, 1, 16, 8, 10, 3, 4};

struct person
{
    char person_name[30];
    int person_age;
}*array[10];

static void insert(struct person array[], char *name, int age) 
{
    static int nextfreeplace = 0;
    array[nextfreeplace] = (struct person*) malloc(sizeof(struct person));
    &array[nextfreeplace]->person_name = name;
    &array[nextfreeplace]->person_age = age;
    nextfreeplace++;  
}

int main(int argc, char **argv) 
{ 
    struct person *array[10];
    int index;
    int personNumber = 1;

    for (index = 0; index < HOW_MANY; index++) 
    {
        insert (array[index], names[index], ages[index]);
    }


    for (index = 0; index < HOW_MANY; index++)
    {
        printf("\nPerson number %d has name %s and age %d \n", personNumber,
             array[index]->person_name, array[index]->person_age);
        personNumber++;
    }

    return 0;
}

The errors I get: 我得到的错误:

arrays.c: In function ‘insert’:
arrays.c:20: warning: implicit declaration of function ‘malloc’
arrays.c:20: warning: incompatible implicit declaration of built-in 
function   ‘malloc’
arrays.c:20: error: incompatible types when assigning to type 
‘struct   person’ from type ‘struct person *’
arrays.c:21: error: invalid type argument of ‘->’ (have ‘struct person’)
arrays.c:22: error: invalid type argument of ‘->’ (have ‘struct person’)
make: *** [arrays] Error 1

You have successfully declared an array of pointers to your struct here 您已在此处成功声明了指向您的结构的指针数组

  struct person *array[10];

You then need to allocate memory such that each pointer in this array actually points to an instance of your struct 然后,您需要分配内存,以便该数组中的每个指针实际上都指向您的结构的实例

Currently you pass 目前您通过

array[index]

to insert as first argument but array[index] is of type person* but your insert function expects person [] . insert作为第一个参数,但array[index]类型为person*但您的insert函数需要person []

You try to access array[nextfreeplace] within your insert function, but as you are passing a person* to insert this will attempt to read a memory address that is potentially not valid because 您尝试在insert函数中访问array[nextfreeplace] ,但是当您传递一个person*进行insert这将尝试读取一个可能无效的内存地址,因为

array[nextfreeplace] = (struct person*) malloc(sizeof(struct person));

is identical to 与...相同

*(array + nextfreeplace) = (struct person*) malloc(sizeof(struct person));

and so whenever nextfreeplace is nonzero you are effectively attempting to allocate dynamic memory and store a pointer to it at an address that you should not be. 因此,只要nextfreeplace不为零,您就在有效地尝试分配动态内存,并将指向它的指针存储在不应使用的地址上。

This leads to potential undefined behaviour. 这导致潜在的不确定行为。 You cannot treat the pointers inside array as if there are arrays themselves (unless you allocate sufficient memory and treat them as such - which is not the case in your code). 您不能像对待数组本身一样对待array内部的指针(除非您分配足够的内存并将其视为此类-代码中不是这种情况)。 You certainly store the return value of malloc to an address at some offset of this pointer. 您当然可以将malloc的返回值存储到此指针某个偏移处的地址。 As your code stands each array[index] is not an array that you can freely read from and write to and should not be treated as such without expecting problems. 就您的代码而言,每个array[index]都不是您可以自由读取和写入的数组,并且在没有出现问题的情况下不应将其视为此类数组。

You might consider changing insert to 您可以考虑将insert更改为

void insert(struct person** p, char *name, int age) 
{
    *p = malloc(sizeof(struct person));
} 

and then changing your loop in main to 然后将main循环更改为

for (index = 0; index < HOW_MANY; index++) 
{
    insert (&array[index], names[index], ages[index]);
}

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

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