2016-10-24 67 views
0

在下面的程序,我试图使功能插件()调用malloc()创建一个新的结构(人)来分配内存...但我得到以下警告:从分配不兼容指针类型[默认情况下启用] ...我应该如何使用malloc()函数?使用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}; 

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


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

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

    person people[7]; 

    for (int i = 0; i < 7; i++) 
    { 
    insert (&people[i], names[i], ages[i]); 
    } 

    for (int i = 0; i < 7; i++) 
    { 
    printf ("name: %s, age: %i\n", people[i].name, people[i].age); 
    } 
    return 0; 
} 
+3

'结构person'和'person'是不一样的。第一个不存在 – ForceBru

+0

[请参阅此讨论,为什么不在'C'中为malloc()'和family生成返回值。](http://stackoverflow.com/q/605845/2173917 )。 –

+0

@roderickyoung当然,我的也走了。 :) –

回答

4

你铸造mallocstruct person *的结果,但pperson *类型。它们不是同一件事。第一个是struct(在这种情况下不存在)的标签名,而另一个是一个typedef其别名(在这种情况下)一个未命名struct

你居然shouldn't be casting the result value of malloc作为可能会导致其他错误。

此外,你不应该使用malloc可言。您传入person结构的地址,因此已经分配了内存(即作为main中的局部变量)。

摆脱malloc的,这将解决这个问题。

+0

当在Visual C++或Builder C++编译器中使用malloc()时,如果结果值不与指针类型一起转换,则总会出现错误。 Visual C++ 6.0:'error C2440:'=':无法从'void *'转换为' *''。 Builder C++ XE10:'E2034 Impossible de convertir'void *'en' *''。 –

+1

@ J.Piquard如果您使用的是C++编译器,那么您需要输出结果。但是,使用C编译器,您不应该这样做。 – dbush

+0

test done Ok ...谢谢 –

1

仔细看看你的代码。

person people[7]; 

peopleperson类型,其中所有的元素都在编译时分配内存的7个元素的数组(严格地讲),这里没有必要运行时间分配。首先,您不需要malloc()

这就是说,如果要动态分配,然后,将需要的person为7个指针变量的阵列,以键入person和可分配存储器给各个指针的insert()函数内。像person *people[7];可以在那里使用。此外,您还需要包含stdlib.h以拥有malloc()和家族的原型。这是why not to cast the return value of malloc() and family in C.的原因之一。

这样,你的函数原型也会改变。你必须

  • 接受person **p
  • (*p)insert()函数内部操作,最后,
  • main(),你必须使用指针成员参考操作->在打印过程中各个元素的值。
+0

我必须做这个程序作为uni的任务...并且在任务中说在insert()函数中使用malloc()... –

+0

@RalucaDamarisLupeş我已经更新了答案,看看是否有帮助。 –

+0

使用 - >打印各个元素的语法是什么? –

0

你可以尝试这样的事情,但这取决于你如何收集数据。但是对于你所描述的阵列来说,这是可能的。

虽然,对于您的具体情况,您已声明7人,最好不要使用malloc()。如果你不知道会有多少人,那么malloc()是个不错的选择。

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

#define HOW_MANY 7 

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

typedef struct { 
    person_t *person; 
    int numpersons; 
} all_person_t; 

all_person_t *initialize_persons(void); 
void insert(all_person_t *persons, char *name, int age, int count); 
void print_persons(all_person_t *persons); 
void insert_each_person(all_person_t *persons, char *names[], int ages[]); 
void free_persons(all_person_t *persons); 

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

    all_person_t *persons; 

    persons = initialize_persons(); 

    insert_each_person(persons, names, ages); 

    print_persons(persons); 

    free_persons(persons); 

    return 0; 
} 

void 
insert_each_person(all_person_t *persons, char *names[], int ages[]) { 
    int i; 

    persons->person = malloc(persons->numpersons *sizeof(person_t)); 

    for (i = 0; i < persons->numpersons; i++) { 
     insert(persons, names[i], ages[i], i); 
    } 
} 

all_person_t 
*initialize_persons(void) { 
    all_person_t *persons; 
    persons = malloc(sizeof(*persons)); 
    persons->numpersons = HOW_MANY; 
    return persons; 
} 

void 
insert(all_person_t *persons, char *name, int age, int count) { 
    persons->person[count].name = malloc(strlen(name)+1); 

    strcpy(persons->person[count].name, name); 
    persons->person[count].age = age; 
} 

void 
print_persons(all_person_t *persons) { 
    int i; 

    for (i = 0; i < persons->numpersons; i++) { 
     printf("%s %d\n", persons->person[i].name, persons->person[i].age); 
    } 
} 

void 
free_persons(all_person_t *persons) { 
    int i; 

    for (i = 0; i < persons->numpersons; i++) { 
     free(persons->person[i].name); 
    } 
    free(persons->person); 
    free(persons); 
} 
+0

@Raluca DamarisLupeş对此有何帮助? – RoadRunner