Skip to Content
Course content

37: Array Initialization Techniques

Click on the "Edit" button in the top corner of the screen to edit your slide content.

By now, you know how to declare an array and access its elements. But in a real project, you're rarely starting with a completely empty array and filling it one by one in a loop. There are several shortcuts in C that make your code cleaner and less prone to "off-by-one" errors during setup.

Do I always have to specify the size of the array when I initialize it?

Short answer: No. If you provide a list of values inside curly braces right at the point of declaration, the compiler is smart enough to count them for you. This is actually my preferred way to do it when the data is static, because if you add an element to the list later, you don't have to remember to update the number inside the square brackets.

// The compiler sees 5 elements, so it makes this an int[5]
int player_scores[] = {1500, 2200, 1800, 900, 3100};

Just keep in mind that this only works during initialization. You can't declare an array and then try to "bulk assign" it later using curly braces; that's a common mistake I see beginners make.

What happens if I only provide a few values for a larger array?

This is a handy little C quirk. If you explicitly define the size of the array but provide fewer initializers than that size, C fills the remaining slots with zeros. This is great for ensuring your data doesn't contain random garbage from memory.

// We want a 10-slot buffer, but we only know the first three values
float sensor_readings[10] = {23.5f, 24.1f, 22.8f}; 

// sensor_readings[0] is 23.5
// sensor_readings[3] through [9] are all automatically 0.0f

I often use this to "zero out" an entire array. If you write int flags[100] = {0};, the first element is explicitly set to 0, and since you didn't provide the other 99, the compiler fills those with 0 as well. It's a clean, one-line way to ensure a fresh start.

Is there a way to initialize specific indices without listing everything in order?

If you're using a modern C compiler (C99 or later), you have access to "designated initializers". This is incredibly useful for things like lookup tables or configuration arrays where most of the values are zero, but a few specific indices need a value. You use the [index] = value syntax inside the braces.

Imagine you're mapping specific keyboard scan codes to game actions. You don't want to write a list of 256 zeros just to set three keys:

// Only index 10, 20, and 50 are set; everything else is zero
int key_map[256] = {
    [10] = 1, // Jump
    [20] = 2, // Attack
    [50] = 3  // Interact
};

It makes the code much more readable because the intent is obvious: "I care about these specific positions, ignore the rest."

Can I use this to fill an array with a value other than zero?

Here is where a lot of people get tripped up. You might try int my_array[10] = {1}; thinking it fills the whole array with ones. It does not. It sets the first element to 1 and the remaining nine to 0.

C just doesn't have a built-in "fill" syntax for non-zero values during initialization. If you need an array full of 1s or -1s, you have two real options: write a simple for loop, or if you're feeling fancy and the array is large, use memset from <string.h> (though be careful with memset and non-char types, as it works byte-by-byte).




📋 Practical Task

Exercise: Configuring a Sparse Game Keymap

You are building a simple input handler for a retro game. The keyboard has 128 possible keys, but the game only uses a handful of them. Instead of using a loop, use designated initializers to create an array named input_config of size 128.

Your task is to initialize the array so that:

  • Index 13 (Enter) is set to 1 (Start Game)
  • Index 32 (Space) is set to 2 (Jump)
  • Index 40 (Left Arrow) is set to 3 (Move Left)
  • Index 41 (Right Arrow) is set to 4 (Move Right)
  • All other indices remain 0.

Write a small main function to print the values of indices 13, 32, and 15 (to verify the 15 is indeed 0) to the console.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.