2017-01-16 97 views
1

我想將數據保存在名爲plist的數組中。這些陣列的大小可能不同,並且是名爲ParticleList的結構的一部分。我知道如何創建一個大小爲n[0]的列表。例如n[0]的大小爲2.因此,大小爲2的列表。但是,如果我想創建多個大小爲n[0], n[1], n[2]ParticleList類型的列表,我該怎麼辦?可變大小的數組結構

爲了削減長話短說:我應該如何修改我的代碼,以訪問可變大小的列表不知何故像pl[numberOfList].plist[PositionInArray] = -1或`PL [numberOfList] - >的plist [PositionInArray] = -1'

#include <stdlib.h> 
#include <stdio.h> 

typedef struct{ 
    double *plist; 
    int plistSize; 
} ParticleList; 

void sendPar(int *n, int nl){ 

    // Allocate memory for struct ParticleList 
    ParticleList *pl = malloc(sizeof(ParticleList)); 

    // Allocate memory for list 
    pl->plist = malloc(sizeof(double)*n[0]); 

    // Fill list with data 
    for(int k=0; k<n[0]; k++){ 
     pl->plist[k] = -1; 
    } 
    // Write size of list into file 
    pl->plistSize = n[0]; 

    // Print data 
    printf("Content of list:\n"); 
    for(int k=0; k<n[0]; k++){ 
     printf("%lf\n", pl->plist[k]); 
    } 
    printf("Size of list: %d\n", pl->plistSize); 


    // Free memory 
    free(pl); 
} 

int main(){ 
    // Number of lists 
    int nl = 3; 

    // Size of lists 
    int n[nl]; 
    n[0] = 2; 
    n[1] = 3; 
    n[2] = 4; 

    sendPar(n, nl); 
} 
+2

_I知道如何創建一個大小爲'n [0]的列表'_...什麼? –

+0

這裏的'sendPar'函數泄漏內存。除此之外,你的問題還不清楚,你應該詳細說明一下。 –

+0

@SouravGhosh'//列表的數量int nl = 3; //列表的大小int n [nl]; n [0] = 2; n [1] = 3; N [2] = 4'。因此,我想說,我知道如何創建一個大小爲[n]的列表,它等於2.因此,列表大小爲2. – Samuel

回答

1

你的意思是這樣的嗎?

typedef struct{ 
    int plistSize; 
    double* plist; 
} ParticleList; 

int main() 
{ 
    int i, z = 0; 

    /* Assuming you have three lists with three different sizes */ 
    double list1[2] = {-1.0, -1.1}; 
    double list2[3] = {-2.0, -2.1, -2.2}; 
    double list3[4] = {-3.0, -3.1, -3.2, -3.3}; 

    /* Create an array of three Particle Lists */ 
    ParticleList pl[3] = {{list1, 2},{list2, 3},{list3, 4}}; 

    /* Access the values in the Particle Lists */ 
    for(i = 0; i < 3; i++) 
    { 
     printf("ParticleList pl[%i]:\n", i); 

     for(z = 0; z < pl[i].plistSize; z++) 
     { 
      printf("pl[%i].plist[%i] = %f\n", i, z, pl[i].plist[z]); 
     } 
    } 

    /* Change the first item of the second list */ 
    pl[1].plist[0] = 2.3;   
} 

這樣你可以在每個列表中

pl[<index of list>].plist[<index of list item>]

更加動感有點用靈活的數組成員(這樣的名單可以被另一個取代訪問每個項目不同大小的列表):

注意我改變了struct!

typedef struct{ 
    int plistSize; 
    double plist[]; 
} ParticleList; 

int main() 
{ 
    int i, z = 0; 
    ParticleList *pl[3]; 

    /* Allocate memory for the lists */ 
    pl[0] = malloc(sizeof(ParticleList) + sizeof(double[2])); 
    pl[0]->plistSize = 2; 
    pl[1] = malloc(sizeof(ParticleList) + sizeof(double[3])); 
    pl[1]->plistSize = 3; 
    pl[2] = malloc(sizeof(ParticleList) + sizeof(double[4])); 
    pl[2]->plistSize = 4; 

    /* Write the values in the Particle Lists */ 
    for(i = 0; i < 3; i++) 
    { 
     printf("ParticleList pl[%i]:\n", i); 

     for(z = 0; z < pl[i]->plistSize; z++) 
     { 
      pl[i]->plist[z] = -i; 
     } 
    } 

    /* Print the values */ 
    for(i = 0; i < 3; i++) 
    { 
     printf("ParticleList pl[%i]:\n", i); 

     for(z = 0; z < pl[i]->plistSize; z++) 
     { 
      printf("pl[%i]->plist[%i] = %f\n", i, z, pl[i]->plist[z]); 
     } 
    } 

    /* Change the first value of the second list */ 
    pl[1]->plist[0] = -1.1; 

    /* Replace the first list by a new one */ 
    free(pl[0]); 
    pl[0] = malloc(sizeof(ParticleList) + sizeof(double[5])); 
    pl[0]->plistSize = 5; 

    /* Assign some new values to the new list 1 */ 
    pl[0]->plist[0] = -4.1; 
    pl[0]->plist[1] = -4.2; 
    pl[0]->plist[2] = -4.3; 
    pl[0]->plist[3] = -4.4; 
    pl[0]->plist[4] = -4.5; 

    /* Print the values */ 
    for(i = 0; i < 3; i++) 
    { 
     printf("ParticleList pl[%i]:\n", i); 

     for(z = 0; z < pl[i]->plistSize; z++) 
     { 
      printf("pl[%i]->plist[%i] = %f\n", i, z, pl[i]->plist[z]); 
     } 
    } 

    /* free all lists before exiting the program */ 
    for(i = 0; i < 3; i++) 
    { 
     free(pl[i]); 
    } 

    return 0; 
} 
+0

這個例子幫助我!在我的程序'list1'到'list3'中可以改變大小。所以我總是有三個列表,但是大小可能會隨時間變化。你的代碼比什麼樣? – Samuel

+0

@Samuel究竟是什麼意思,列表的大小可能隨時間而改變?如果列表正在增長或縮小,但仍然保留舊的價值?或者完整的列表被另一個不同大小的列表所取代? – gmug

+0

當我在一個時間步中使用'list1'到'list3'後,我刪除它們以釋放內存。然後在下一個時間步驟中確定列表的新長度。所以我必須爲三個新列表分配內存。 – Samuel

1

看起來您正在尋找稱爲靈活陣列成員的語言功能。它的工作原理是這樣的:

typedef struct{ 
    int plistSize; 
    double plist[]; 
} ParticleList; 

ParticleList *pl = malloc(sizeof(ParticleList) + sizeof(double[n])); 
pl->plistSize = n; 
... 
free(pl); 

哪裏n是你想要plist有大小。

+0

沒關係。這個問題不斷變化。 – Lundin

+0

我可以使用它並訪問諸如'pl [numberOfList] .plist [positionInArray]'的列表嗎? – Samuel

+0

@Samuel否,因爲'pl'不是一個數組。如果你想要它是一個數組,你必須使它成爲一個指針數組''ParticleList * pl = malloc(sizeof(ParticleList * [something]));'其中每個指針指向一個動態分配的項目'pl [i] = malloc(sizeof(ParticleList)+ sizeof(double [n]));'。然後你可以使用這個符號。 – Lundin