2012-10-31 142 views
2

您好,我在定義結構中的結構數組時遇到了一些麻煩。 這是我的想法,我需要一個名爲figure的結構,它包含圖形的名稱,座標計數和座標(x,y)。每個圖可以有任意數量的座標。 我還需要能夠動態地重新分配空間以獲得不斷增加的座標列表......請幫助指引我朝着正確的方向前進。 謝謝你,在C中定義一個結構中的結構陣列C

泰勒

typedef struct { 
    char fig_name[FIGURE_LEN + 1]; 
    int coordcount; 
    /* here i need to declare an array of coord structures that 
    but i am not sure how to do this properly. I was originally 
    going to try something like as follows */ 
    coords *pointer; 
    pointer = malloc(sizeof(coords)); 
    pointer = coords figcoord[]; 
    /* however i am quite certain that this would not work */ 
} figure; 

typedef struct { 
    double x; 
    double y; 
} coords; 
+2

您不能將默認結構值分配給C中的成員變量。您的* code *隱藏在結構定義的中間。在可以使用之前,必須先聲明座標,否則不會在C. – WhozCraig

+0

中工作。另外,C是一種靜態類型語言,因此您需要先聲明才能進行分配。 – yeyo

+0

@Kira是的,我認爲這是一個問題,我只是不知道如何解決這個問題。 – wenincode

回答

1

踢朝着正確的方向。嘗試這樣的事情。我爲缺乏錯誤檢查的malloc()電話道歉,但你會得到的總體思路(我希望):

#include <stdlib.h> 

#define FIGURE_LEN 128 

typedef struct 
{ 
    double x; 
    double y; 
} coords; 

typedef struct 
{ 
    char fig_name[FIGURE_LEN + 1]; 
    int coordcount; 
    coords *pointer; 
} figure; 


/* allocate a dynamic allocated figure */ 
figure* alloc_figure(char* name, int coordcount) 
{ 
    figure *fig = malloc(sizeof(figure)); 
    fig->coordcount = coordcount; 
    fig->pointer = malloc(sizeof(coords) * coordcount); 
    strncpy(fig->fig_name, name, FIGURE_LEN); 
    fig->fig_name[FIGURE_LEN] = 0; 
    return fig; 
} 

/* release a dynamic allocated figure */ 
void free_figure(figure** ppfig) 
{ 
    if (!*ppfig) 
     return; 

    free((*ppfig)->pointer); 
    free(*ppfig); 
    *ppfig = NULL; 
} 

int main(int argc, char *argv[]) 
{ 
    figure fig; 
    fig.coordcount = 10; 
    fig.pointer = malloc(10 * sizeof(coords)); 

    /* access fid.pointer[0..9] here... */ 
    fig.pointer[0].x = 1.0; 
    fig.pointer[0].y = 1.0; 

    /* don't forget to free it when done */ 
    free(fig.pointer); 

    /* dynamic allocation function use */ 
    figure *fig1 = alloc_figure("fig1", 10); 
    figure *fig2 = alloc_figure("fig2", 5); 

    fig1->pointer[9].x = 100.00; 
    fig2->pointer[0].y = fig1->pointer[9].x; 

    /* and use custom free function for releasing them */ 
    free_figure(&fig1); 
    free_figure(&fig2); 

    return EXIT_SUCCESS; 
} 
+0

謝謝,這看起來像我正在尋找什麼。 – wenincode

1

我覺得這樣的事情應該工作。

typedef struct { 
    char* figNamePtr; 
    int coordCount; 
    Coords *cordsPointer; 
    //void (*ReleaseMemory)(); // create this function. 
} Figure; 

typedef struct { 
    double x; 
    double y; 
} Coords; 

Figure * NewFigure(int coordCount){ 
    Figure * retVal = (Figure *) malloc(sizeof(Figure)); 

    /* Initialize Figure */ 
    retVal->figNamePtr = (char *) malloc(sizeof(char) * (FIGURE_LEN + 1)); 

    /* Set the Function pointer, 
     create a function named ReleaseMemory that free up the memory */ 
    //retVal->ReleaseMemory = ReleaseMemory; 

    retVal->cordCount = coordCount 

    retVal->cordsPointer = malloc(sizeof(Coords) * coordCount); 

    return retVal; 
} 

有了這個代碼,當你想創建一個新的圖,只是調用的函數NewFigure()必要的參數。完成使用後,請撥打ReleaseMemory()

希望它有幫助!