19

I want to have two arrays in a struct, which are initialized at start but need editing further on. I need three instances of the struct, so that I can index into a specific struct and modify as I wish. Is it possible?

This is what I thought I could do but I get errors:

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

Then I access the structs as follows:

 printf("some statement %s", aPot[0].array[0]);
 aPot[0].theName[3];
 …
2
  • 7
    What errors do you get? Commented Mar 2, 2011 at 16:38
  • 4
    Is that really how your struct is defined? Commented Mar 2, 2011 at 16:46

2 Answers 2

18

The struct themselves do not have data. You need to create objects of the struct type and set the objects ...

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";
/* ... */
Sign up to request clarification or add additional context in comments.

1 Comment

more docs on designated initializers and their gcc extensions: gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
14

In C struct array elements must have a fixed size, so the char *theNames[] is not valid. Also you can not initialize a struct that way. In C arrays are static, i.e. one cannot change their size dynamically.

A correct declaration of the struct would look like the following

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

and you initialize it like this:

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 */ }
    }
};

But, I'm pretty sure this is not what you want. The sane way to do this required some boilerplate code:

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;
};

Personally I strongly advise against using naked C arrays. Doing everything through helper structs and functions keeps you clear from buffer under/overruns and other trouble. Every serious C coder builds a substancial code library with stuff like this over time.

5 Comments

ok thanks for your help, could you give me an example of how i would enter the data, and how i would then be able to access it?
downvoted because Flexible Array Members have been available since C99, 12 years before this answer was written.
@JanusTroelsen: If you knew how Flexible Array Members worked, you'd not written this ill informed comment (and did downvote). FAMs only work for the very last element in a structure and come with several caveats. OP has to deal with more than one array of varying length. FAMs can't do that.
@datenwolf even if FAMs are not applicable here, the sentence "In C struct array elements must have a fixed size" is wrong in the presence of FAMs.
@JanusTroelsen: Even with FAMs only the very last element in a struct is permitted to be a FAM. Everything that's not the last element of a struct, including arrays must be fixed size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.