1

I am learning C and some things confuse me and the books I have read didn't really help in clarifying the problem I have.

So here is the code I have:

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

#define ARRAY_SIZE 5

// gcc -std=c99 stackoverflow-example.c

int main () {

    // declare variable array1
    int array1[ARRAY_SIZE];
    // declare and init variable array2
    int array2[ARRAY_SIZE] = {}; // for integers, the default value is 0

    // not initialized
    for (int i = 0; i < ARRAY_SIZE; i++) {
        // can be anything, not guaranteed to be 0
        printf("array1[%d]: %d\n", i, array1[i]);
    }

    // initialized with initialization list
    for (int i = 0; i < ARRAY_SIZE; i++) {
        // element == 0
        printf("array2[%d]: %d\n", i, array2[i]);
    }

    // This is the part that confuses me.
    // array1 = {};  // error: expected expression before ‘{’ token
    // array1[] = {};  // same error

    return EXIT_SUCCESS;
}

Is there a handy way to initialize this array after its declaration? Or the only way to set every element in array1 is with a for loop, e.g.:

for (int i = 0; i < ARRAY_SIZE; i++)
    array1[i] = 0;

// initialized with a for loop
for (int i = 0; i < ARRAY_SIZE; i++)
    // now it's guaranteed to be 0
    printf("array1[%d]: %d\n", i, array1[i]);

I really appreciate your help. I know it's somewhat of a noob question, but it came up as I was trying to get more comfortable with the C language and tried some non-book-example code.

If you suspect, that there might be something fundamental that I didn't get, please let me know, I'll read up on it.

4
  • I think you should try the memset function. It is optimized to initialize blocks of data with a constant value. Commented Oct 31, 2016 at 16:04
  • Ok, thanks. Though, it works only for initializing with 0 (know I used 0 initialized array in my example, so it'd work actually). and -1 (and it's not really guaranteed). Thank you, nonetheless. Commented Oct 31, 2016 at 16:31
  • Oh, my bad. You're right, memset is specific for arrays of bytes. Commented Oct 31, 2016 at 17:05
  • in C, a variable (array or struct or ...) can only be 'initialized' when it is declared. Thereafter, it must be assigned. When assigning, without using functions like memset(), each entity must be assigned to. Commented Nov 1, 2016 at 4:41

1 Answer 1

1

Technically speaking, initialization can be done once and only at declaration time, any value storing after that is assignment or copying.

A brace-enclosed initializer list can be used for initialization of arrays only at the declaration time.

For an individual element of an array, (a scalar item), the rule is: (quoting C11, chapter §6.7.9)

The initializer for a scalar shall be a single expression, optionally enclosed in braces.

and an empty list {} is not a valid initializer (expression) for a scalar. hence you got the error.

So, for an already defined array, the re-setting has to be done either

  • member-by-member, via a loop
  • using memcpy(), or memset if so permits.
Sign up to request clarification or add additional context in comments.

5 Comments

Then let me rephrase: Is there a way (kind of like a "syntactic sugar", though I know this is not what C is famous about) to assign various elements of an array after declaration?
@VinceVarga well, memset() may be your friend, but not for different values, I fear.
memset() will set all the bytes to the same value, which is probably not much use if you want to set integer elements to the same non-zero value. You can usually get away with using memset() for setting all the elements to an integer 0, but strictly speaking, that is non-portable. For setting elements to the same non-zero value, a loop is probably your best bet. For setting elements to different values, it can be done with individual assignments, or by using memcpy() to copy everything over from another (possibly const) array.
In addition to what I wrote about memcpy() in my previous comment, the second (source) parameter of memcpy() could be a compound literal array, e.g. memcpy(array1, (int[ARRAY_SIZE]){ 1, 2, 3, 4, 5}, ARRAY_SIZE * sizeof(int));
Thank you, Ian, it appears to me that the memcpy is the closes thing I got to what I wanted originally

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.