Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring and initializing arrays in C

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....}; 
like image 480
Dave H Avatar asked Jun 29 '10 03:06

Dave H


People also ask

How do you declare and initialize arrays in C?

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.

How do you declare and initialize arrays?

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};

How do you declare an array in C?

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.

What is array initialisation in C?

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.


2 Answers

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); 
like image 92
AnT Avatar answered Oct 06 '22 01:10

AnT


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 :-).

like image 29
paxdiablo Avatar answered Oct 06 '22 00:10

paxdiablo