2014-09-11 2 views
-2

나는이 구조체가 정의되어 내 주요 기능에C 프로그래밍 호출 포인터의 포인터

typedef struct { 
    char *first_name; 
    char *last_name; 
    char SSN[9]; 
    float gpa; 
    struct student *next; 
} student; 

typedef struct { 
    student *head; 
    student *current; 
    student *tail; 
} students; 

, 나는 학생들 구조체에 학생을 추가 할 수 있습니다. 그리고 head student의 first_name을 호출하십시오. 어떻게해야합니까?

void add(students *list, student *a) { 
    if(list->head) { 
     a->next = NULL; 
     list->head = &a; 
     list->current = list->head; 
     list->tail = NULL; 
    } else { 
     printf("Will implement"); 
    } 
} 

int main() 
{ 
    students grade1; 

    student a; 
    a.first_name = &"Misc"; 
    a.last_name = &"Help"; 

    add(&grade1, &a); 

    printf("%s %s", a.first_name, a.last_name); 
    printf("%s", grade1.head->first_name); 
} 

printf ("% s", grade1.head-> first_name);

작동하지 않는 것 같습니다. 어떤 제안?

감사

모든
+2

'작동하지 않는 것 같습니다.'작동하지 않는 것은 무엇입니까? 뭘 원해? 이 코드가 컴파일됩니까? 문제에 대해 구체적으로 설명하십시오. –

+1

'list-> head = & a;'행에 잘못된 유형에 대한 경고가 표시되지 않습니까? 새로운 학생이 목록의 머리에 놓여지면 목록의 오래된 머리에 무엇인가 놓을 필요가 없으므로 잃지 않도록하십시오. – Barmar

+0

나는 grade1.head-> first_name을 반환하기를 원한다. – Raza

답변

0

먼저 당신은 객체 grade1의 모든 데이터 멤버를 초기화해야합니다.

students grade1 = { 0 }; 

두 번째로 대신 예를 들어

student a; 
a.first_name = &"Misc"; 
a.last_name = &"Help"; 

기능 add

void add(students *list, student *a) 
{ 
    if (list->tail == NULL) 
    { 
     list->head = list->tail = a; 
    } 
    else 
    { 
     list->tail->next = a; 
    } 
} 

처럼 보일 수 그리고 호출 할 수있는 적어도

student *a = malloc(sizeof(student)); 
a->first_name = "Misc"; 
a->last_name = "Help"; 
a->next = NULL; 

처럼이 있어야한다 as

add(&grade1, a);