Is there a way to declare first and then initialize an array in C?
So far I have been initializing an array like this:
int myArray[SIZE] = {1,2,3,4....};
But I need to do something like this
int myArray[SIZE]; myArray = {1,2,3,4....};
Array Initialization Using a Loop This is the most common way to initialize an array in C. // declare an array. int my_array[5]; // initialize array using a "for" loop.
We declare an array in Java as we do other variables, by providing a type and name: int[] myArray; To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15};
To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100}; We have now created a variable that holds an array of four integers.
The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.
In C99 you can do it using a compound literal in combination with memcpy
memcpy(myarray, (int[]) { 1, 2, 3, 4 }, sizeof myarray);
(assuming that the size of the source and the size of the target is the same).
In C89/90 you can emulate that by declaring an additional "source" array
const int SOURCE[SIZE] = { 1, 2, 3, 4 }; /* maybe `static`? */ int myArray[SIZE]; ... memcpy(myarray, SOURCE, sizeof myarray);
No, you can't set them to arbitrary values in one statement (unless done as part of the declaration).
You can either do it with code, something like:
myArray[0] = 1; myArray[1] = 2; myArray[2] = 27; : myArray[99] = -7;
or (if there's a formula):
for (int i = 0; i < 100; i++) myArray[i] = i + 1;
The other possibility is to keep around some templates that are set at declaration time and use them to initialise your array, something like:
static const int onceArr[] = { 0, 1, 2, 3, 4,..., 99}; static const int twiceArr[] = { 0, 2, 4, 6, 8,...,198}; : int myArray[7]; : memcpy (myArray, twiceArr, sizeof (myArray));
This has the advantage of (most likely) being faster and allows you to create smaller arrays than the templates. I've used this method in situations where I have to re-initialise an array fast but to a specific state (if the state were all zeros, I would just use memset
).
You can even localise it to an initialisation function:
void initMyArray (int *arr, size_t sz) { static const int template[] = {2, 3, 5, 7, 11, 13, 17, 19, 21, ..., 9973}; memcpy (arr, template, sz); } : int myArray[100]; initMyArray (myArray, sizeof(myArray));
The static array will (almost certainly) be created at compile time so there will be no run-time cost for that, and the memcpy
should be blindingly fast, likely faster than 1,229 assignment statements but very definitely less typing on your part :-).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With