2017-01-16 4 views
1

plistという配列にデータを保存したいとします。これらの配列はサイズが異なり、ParticleListという構造体の一部です。私はサイズn[0]の1つのリストを作成する方法を知っています。 n[0]のサイズは2です。従って、サイズ2のリストです。サイズParticleListのサイズn[0], n[1], n[2]のリストをいくつか作成したいのですが、どうすればいいですか? - > 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

私はサイズのリストを作成する方法を知っています[0] '_...何? –

+0

ここにある 'sendPar'関数はメモリをリークしています。それ以外はあなたの質問がはっきりしていないので、少し詳しく説明してください。 –

+0

@SouravGhosh '//リストの数int nl = 3; //リストのサイズint n [nl]; n [0] = 2; n [1] = 3; n [2] = 4 'となる。したがって、私は、サイズ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>]

もう少し動的によって各リスト内の各項目にアクセスすることができ、この方法別のサイズのリスト):

私は構造体を変更しました!

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'へのサイズ変更が可能です。だから私は常に3つのリストを持っていますが、サイズは時間の経過とともに変化するかもしれません。あなたのコードはどのように見えるでしょうか? – Samuel

+0

@Samuel正確には、リストのサイズが時間の経過と共に変化する可能性がありますか?リストが大きくなったり縮小したりしても、古い値を保持している場合は、または、完全なリストが異なるサイズの別のリストに置き換えられていますか? – gmug

+0

'list1'から' list3'までを一度に実行した後、それらを削除してメモリを解放します。次に、次のステップでは、リストの新しい長さを決定します。だから私は3つの新しいリストのためにメモリを割り当てる必要があります。 – Samuel

1
どのように私は何とか pl[numberOfList].plist[PositionInArray] = -1または `PL [numberOfList]のように可変サイズのリストにアクセスするために自分のコードを変更する必要があります。長い話を短くカットする

というフレキシブルな配列メンバーという言語機能を探しているようです。 nあなたはplistを持つようにしたいサイズです

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

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

:それはこのように動作します。

+0

心配しないでください。質問は変わり続ける。 – Lundin

+0

これを使用して 'pl [numberOfList] .plist [positionInArray]'のようなリストにアクセスできますか? – Samuel

+0

@Samuelいいえ 'pl'は配列ではないからです。それを配列にするには、ポインタ '' ParticleList * pl = malloc(sizeof(ParticleList * [something]); ''の配列にする必要があります。 malloc(sizeof(ParticleList)+ sizeof(double [n])); '。その後、その表記法を使用することができます。 – Lundin