web-dev-qa-db-fra.com

Tableau en structure C

Je veux avoir deux tableaux dans une structure, qui sont initialisés au début mais doivent être modifiés plus loin. J'ai besoin de trois instances de la structure, afin de pouvoir indexer dans une structure spécifique et modifier comme je le souhaite. C'est possible?

C'est ce que je pensais pouvoir faire mais j'obtiens des erreurs:

struct potNumber{
    int array[20] = {[0 ... 19] = 10};
    char *theName[] = {"Half-and-Half", "Almond", "Rasberry", "Vanilla", …};
} aPot[3];

Ensuite, j'accède aux structures comme suit:

 printf("some statement %s", aPot[0].array[0]);
 aPot[0].theName[3];
 …
14
Greg

La structure elle-même n'a pas de données. Vous devez créer des objets du type struct et définir les objets ...

struct potNumber {
    int array[20];
    char *theName[42];
};

/* I like to separate the type definition from the object creation */
struct potNumber aPot[3];
/* with a C99 compiler you can use 'designated initializers' */
struct potNumber bPot = {{[7] = 7, [3] = -12}, {[4] = "four", [6] = "six"}};

for (i = 0; i < 20; i++) {
  aPot[0].array[i] = i;
}
aPot[0].theName[0] = "Half-and-Half";
aPot[0].theName[1] = "Almond";
aPot[0].theName[2] = "Rasberry";
aPot[0].theName[3] = "Vanilla";
/* ... */

for (i = 0; i < 20; i++) {
  aPot[2].array[i] = 42 + i;
}
aPot[2].theName[0] = "Half-and-Half";
aPot[2].theName[1] = "Almond";
aPot[2].theName[2] = "Rasberry";
aPot[2].theName[3] = "Vanilla";
/* ... */
13
pmg

Dans les éléments du tableau struct C doivent avoir une taille fixe, donc le char *theNames[] n'est pas valide. Vous ne pouvez pas non plus initialiser une structure de cette façon. En C, les tableaux sont statiques, c'est-à-dire qu'on ne peut pas changer leur taille dynamiquement.

Une déclaration correcte de la structure ressemblerait à ce qui suit

struct potNumber{
    int array[20];
    char theName[10][20];
};

et vous l'initialisez comme ceci:

struct potNumber aPot[3]=
{
    /* 0 */
    { 
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ },
        {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ }
    },
    /* 1 */
    { 
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ },
        {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ }
    },
    /* 2 */
    { 
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 /* up to 20 integer values*/ },
        {"Half-and-Half", "Almond", "Raspberry", "Vanilla", /* up to 10 strings of max. 20 characters */ }
    }
};

Mais je suis presque sûr que ce n'est pas ce que vous voulez. La manière sensée de le faire nécessitait du code passe-partout:

struct IntArray
{
    size_t elements;
    int *data;
};

struct String
{
    size_t length;
    char *data;
};

struct StringArray
{
    size_t elements;
    struct String *data;
};
/* functions for convenient allocation, element access and copying of Arrays and Strings */

struct potNumber
{
    struct IntArray array;
    struct StringArray theNames;
};

Personnellement, je déconseille fortement d'utiliser des tableaux C nus. Tout faire par le biais de structures et de fonctions d'assistance vous évite les dépassements/dépassements de tampon et autres problèmes. Chaque codeur C sérieux construit une bibliothèque de code substantielle avec des trucs comme ça au fil du temps.

9
datenwolf