指向结构的指针数组

时间:2017-11-06 17:32:01

标签: c arrays pointers struct

我是C的菜鸟。我写了一个程序,我的数组人员现在是一个指向结构的指针数组。并插入以调用malloc以创建新结构并设置指向它的正确数组元素。 这是我的代码。

#include <stdio.h>   
/* these arrays are just used to give the parameters to 'insert',   
   to create the 'people' array
*/

#define HOW_MANY 7 char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim","Harriet"}; 
int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};

/* declare your struct for a person here */
typedef  struct{   
    char*  name;
    int   age;
} person;


static void insert( person *people[], char *name, int age,int i) {  
    /* put name and age into the next free place in the array parameter
       here */
    people[i] = malloc(sizeof(person));
    people[i]->name = name;
    people[i]->age = age;
    /* modify nextfreeplace here */
}

int main(int argc, char **argv) {

    /* declare the people array here */   
    person *people[7];

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

    /* print the people array here*/
    for(int index=0;index < HOW_MANY;index=index+1)     {
        printf("name: %s, age: %i\n",
               people[index]->name, people[index]->age);
    }
    return 0;
}

我得到错误

  

part2.c:23:5:警告:隐式声明函数'malloc'[-Wimplicit-function-declaration]

       people[i] = malloc(sizeof(person));
        ^ part2.c:23:17: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
     people[i] = malloc(sizeof(person));

任何人都可以帮助我吗?欣赏

2 个答案:

答案 0 :(得分:0)

请在下面查看我的答案,并与需要修改的代码进行比较,并尝试了解我的代码在做什么。如果您不清楚我的代码的任何部分,请随时向我提问。

    char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim","Harriet"};
    int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};

    /* declare your struct for a person here */

    typedef  struct{
        char*  name;
        int   age;
    } person;

    static void insert( person *people[], char *name, int age,int i) {
        /* put name and age into the next free place in the array parameter
           here */
        people[i] = malloc(sizeof(person));
        people[i]->name = name;
        people[i]->age = age;
        /* modify nextfreeplace here */
    }

    int main(int argc, char **argv) {

        /* declare the people array here */
        person *people[7];

        for (int index=0;index < HOW_MANY;index=index+1)   {
            insert(people, *(names+index), ages[index],index);
        }

        /* print the people array here*/
        for(int index=0;index < HOW_MANY;index=index+1)     {
            printf("name: %s, age: %i\n",
                   people[index]->name, people[index]->age);
        }
        return 0;
    }

答案 1 :(得分:0)

我发现了一些导致这些问题的问题。检查我在下面给出的代码。 在评论中,我已经提到了在哪里进行更改。

您正在使用malloc函数而不包含stdlib文件! 看到这段代码,我已经在dev-cpp中对它进行了测试,现在它正在运行。

#include <stdio.h>   
#include <stdlib.h>
/* these arrays are just used to give the parameters to 'insert',   
   to create the 'people' array
*/

#define HOW_MANY 7 
char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim","Harriet"}; 
int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};

/* declare your struct for a person here */
typedef  struct{   
    char*  name;
    int   age;
} person;

/* declare the people array here */   
person *people[7];

person* insert(char *name, int age,int i) {  
    /* put name and age into the next free place in the array parameter
       here */

    people[i] = malloc(sizeof(person));
    people[i]->name = name;
    people[i]->age = age;

    /* modify nextfreeplace here */
}

int main(int argc, char **argv) {    

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

    /* print the people array here*/
    for(index=0;index < HOW_MANY;index=index+1)     {
        printf("name: %s, age: %i\n",
               people[index]->name, people[index]->age);
    }
    return 0;
}