Skip to content

Instantly share code, notes, and snippets.

@carimatics
Last active November 1, 2016 01:24
Show Gist options
  • Save carimatics/465c02c58b957a0b5a6ff04faf9245fb to your computer and use it in GitHub Desktop.
Save carimatics/465c02c58b957a0b5a6ff04faf9245fb to your computer and use it in GitHub Desktop.
Initializing Arrays in C language
/* see also: https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html */
// assign by index bracket
int nums[5] = { [2] = 10 }; //=> nums: { 0, 0, 10, 0, 0}
int nums[5] = { [2] = 10, [4] = 20} //=> nums: { 0, 0, 10, 0, 20}
// assign by range
int nums[5] = { [0 ... 4] = 0}; //=> nums: { 0, 0, 0, 0, 0}
int nums[5] = { [0 ... 2] = 1, 2, 3}; //=> nums: { 1, 1, 1, 2, 3}
// assign default values if you don't specify
int nums[5] = { 0 }; //=> nums: { 0, 0, 0, 0, 0}
int nums[5] = {}; //=> nums: { 0, 0, 0, 0, 0}
int nums[5] = {0, 1, 2, [4] = 4} //=> nums: { 0, 1, 2, 0, 4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment