나는 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 기능 선언]의 암시 적 선언
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));
아무도 도와 줄 수 있습니까? 감사합니다
이 질문은 읽을 수 없습니다. –
코드를 복사하여 붙여 넣기 하시겠습니까? 그냥 그대로 복사하여 붙인 다음 코드를 선택하고 현장 툴바의'{}'버튼을 눌러 코드로 포맷하십시오. –
'#include ' –
sidyll