2017-11-06 4 views
-1

나는 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)); 

아무도 도와 줄 수 있습니까? 감사합니다

+1

이 질문은 읽을 수 없습니다. –

+0

코드를 복사하여 붙여 넣기 하시겠습니까? 그냥 그대로 복사하여 붙인 다음 코드를 선택하고 현장 툴바의'{}'버튼을 눌러 코드로 포맷하십시오. –

+4

'#include ' – sidyll

답변

0

이러한 문제를 일으키는 몇 가지 문제가 있습니다. 아래에 주어진 코드를 확인하십시오. 의견에 나는 어디에서 변경해야하는지 언급했습니다.

stdlib 파일을 포함하지 않고 malloc 기능을 사용 중입니다! 이 코드를 읽고 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; 
} 
+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; 
    } 
+0

많은 도움을 청합니다. 알겠습니다. –

+0

참조 stackoverflow.com/help/accepted-answer 및 meta.stackexchange.com/q/5234/204869 –

관련 문제