구조체를 리턴해야 하는 경우는 다음의 경우를 이용하는 것이 좋다.

 

출처: http://kin.naver.com/db/detail.php?d1id=1&dir_id=10104&eid=0tBeloUK0KfHnV5fAL4TxnUrkk8EdSo/ 

 


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define Link_Num 18

//구조체 선언
typedef struct _arc
{
    int n1; //노드 x
    int n2; //노드 y
    int value; //링크수준
}Arc;


//함수 선언
Arc* Empty_Arc(void);

int main()
{
    int i;
    Arc *temp_arc;

    temp_arc = Empty_Arc();


    for(i=0 ; i < Link_Num ; i++)       
 printf("체크 %d\n", temp_arc[i].value);  //결과2

    free(temp_arc);
    system("pause");
    return 0;
}

Arc* Empty_Arc(void)
{
    int i, j;
    Arc* temp = (Arc*)malloc(sizeof(Arc) * Link_Num);

    //초기화
    for (i=0 ; i < Link_Num ; i++)    {       
 temp[i].n1 = 0;
        temp[i].n2 = 0;
        temp[i].value = i;
    }

    for(i=0 ; i < Link_Num ; i++)       
 printf("리턴값 %d\n", temp[i].value);  //결과1

    return temp;
}

Posted by '김용환'
,