3

I have this struct definition:

typedef struct intArray
{
    int myArray[1000];
} intArray;

My goal is to create an intArray of zeros, i tried this:

intArray example;
int createArray[1000] = {0};
example.myArray = createArray;

This results in this error message:

error: assignment to expression with array type

I want the struct to automatically initialize the array to 0's but i understand it is not possible because it is only a type definition and not a variable. So i created one and created the array, just tried to assign it and this is the result. Any advice is appreciated.

Eyzuky
  • 1,843
  • 2
  • 22
  • 45

2 Answers2

1

Declaring arrays like int myArray[1000]; won't let you change the value of the array pointer. Declare your struct like

typedef struct intArray
{
    int *myArray;
} intArray;

if you can.

Awais Chishti
  • 395
  • 2
  • 19
  • This would avoid the given error, I think, but doesn't it open you up to potentially worse runtime issues? For instance, if you declare an array locally and assign it to the pointer, then once you exit the scope where the array was declared, it would be deallocated, and I'm not sure what would happen when you try to access that memory via the pointer. – Dave Costa Aug 13 '16 at 13:48
  • If he wants to access the array outside it's scope, shouldn't he declare it with `malloc` in the first place? – Awais Chishti Aug 13 '16 at 13:51
  • But in this way of declaring (int *myArray) i cannot control the maximum ammount of ints. Should i do it and then allocate the memory using malloc once i create the struct? – Eyzuky Aug 13 '16 at 13:53
  • 1
    The amount of `int`s is decided by your declaration of `createArray`. `example.myArray = createArray;` will just copy the address of that declared memory to `myArray`. – Awais Chishti Aug 13 '16 at 13:57
  • 1
    And yes, you can allocate the memory with `malloc()` like `example.myArray = malloc(sizeof(int)*1000);` after declaring the struct (if that's what you were asking). – Awais Chishti Aug 13 '16 at 14:00
1

Why not use memset to zero the array? Also, as suggested by another user, it'd be better to allocate this memory to a pointer....especially if you intend to pass that struct around between functions.

Just a thought, but this would work:

typedef struct intArray {
    int *myArray;
} intArray;

int main(void)
{
    intArray a;
    int b;

    // malloc() and zero the array
    //         
    // Heh...yeah, always check return value -- thanks,
    // Bob__ - much obliged, sir.
    //              
    if ((a.myArray = calloc(1000, sizeof *a.myArray)) == NULL) {
        perror("malloc()");
        exit(EXIT_FAILURE);
    }

    memset(a.myArray, 0, (1000 * sizeof(int)));

    // Fill the array with some values
    //
    for (b = 0; b < 1000; b++)
        a.myArray[b] = b;

    // Just to make sure all is well...yep, this works.
    //
    for (b = 999; b >= 0; b--)
        fprintf(stdout, "myArray[%i] = %i\n", b, a.myArray[b]);

    free(a.myArray);

}
Nunchy
  • 948
  • 5
  • 11
  • Consider using [`calloc()`](http://linux.die.net/man/3/calloc) instead: `a.myArray = calloc(1000, sizeof *a.myArray);`. Also, remember to check the return value. – Bob__ Aug 13 '16 at 14:25
  • Duly noted, muchas gracias. – Nunchy Aug 13 '16 at 14:28
  • Ehm, the point of using `calloc()` is that it [already zeroes all the bits...](http://stackoverflow.com/a/1538427/4944425) ;) – Bob__ Aug 13 '16 at 14:34
  • Heh...yeah - that'd significantly shorten my answer. – Nunchy Aug 13 '16 at 14:41