Last active
November 1, 2016 01:24
-
-
Save carimatics/465c02c58b957a0b5a6ff04faf9245fb to your computer and use it in GitHub Desktop.
Initializing Arrays in C language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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