How to reinitialize an array in pure C exactly (not C ++)?

How can I reinitialize an array in pure C?

How can I create it to reinitialize it in the next step in my code?
Should I determine through a pointer it: * int data [] or can it just encode: int data [] = {};
And somehow reinitialize it in the forward steps in my code?

For example, I defined it as:

int main(void) { int data[] = { 44, 22, 1, 2, 3, 1000, 3 }; 

Best wishes,
Thanks

+4
source share
4 answers

Initialization occurs only once when you create an array:

 int foo[] = {0,1,2,3,4}; // creates a 5-element array of int and // initializes it 

After defining the array, you can assign individual elements:

 foo[0] = 5; foo[1] = 4; foo[2] = 3; foo[3] = 2; foo[4] = 1; 

but you cannot assign to the array itself; IOW, you cannot write something like

 foo = {5,4,3,2,1}; 

However, you can use memcpy to copy the contents of one array to another:

 int foo[5]; int bar[5] = {1,2,3,4,5}; int i; memcpy(foo, bar, sizeof bar); // Copies *contents* of bar to foo for (i = 0; i < sizeof foo / sizeof *foo; i++) printf("foo[%d] = %d\n", i, foo[i]); 

Likewise, an array created in this way cannot be modified; its size is fixed during the announcement. In C89 and earlier, the size of the array must be known at compile time β€” either by specifying a size with a compile-time constant (an integral expression or a macro that has been expanded to an integral expression), or using an initializer, such as the one above, from which the size of the array is calculated.

C99 introduced variable-length arrays that can be declared using a run-time value, for example:

 void foo(int x) { int arr[x]; ... } 

Please note that, like regular arrays, VLAs cannot be modified after they are defined.

Alternatively, you can dynamically allocate an array using malloc , although you cannot initialize it as follows:

 int *foo = malloc(sizeof *foo * N); // where N is the number of elements 

You can still assign to individual elements:

 foo[0] = 5; foo[1] = 4; foo[2] = 3; ... 

or you can use memcpy as shown above. Note that you must remember the free array when you are done:

 free(foo); 

Arrays created in this way can be modified using realloc :

 int *tmp = realloc(foo, sizeof *foo * NEW_NUMBER_OF_ELEMENTS); if (tmp) foo = tmp; 

Why not just assign the realloc result back to foo ? If the realloc operation failed, it will return NULL. If this happens, and we return the result back to foo , we will lose the memory of the already allocated memory, which will lead to a memory leak.

C99 introduced array literal syntax; you can write something like

 int *foo = (int[]){1,2,3,4,5}; 

and then index in foo as an array:

 printf("foo[%d] = %d\n", i, foo[i]); 

although I'm sure you cannot change the contents of foo[i] , much like trying to change the contents of a string literal undefined (although I did not find a chapter and a verse on this).

+10
source

You cannot reinitialize any types as such, not arrays.
Initialization is a one-time event when you provide some value to a variable in the same expression when it is created.

You can explicitly skip the array and fill it again if you need to assign new values ​​to each of them
OR
Use memset () or memcpy () to set all items to a specific value or to copy specific items or.

+4
source

Initialization (if any) must be performed at the time the variable is created. Because at compile time, the compiler assigns this value to a variable. Any other assignments after this line are executed only at runtime.

In your case, you can initialize the array during its creation. The next time you assign a value, it does not mean "initialization"; rather, it is just a run-time assignment.

If you want to create and store elements dynamically, you can use pointers to do this.

+1
source

My partial solution for assigning new values ​​(works in gcc with c99 -syntax):

 #define arr_set(type, arr, ...) { \ int arr_size = sizeof((type[]){__VA_ARGS__}) / sizeof(type); \ type *parr = (type[]){__VA_ARGS__}; \ for (int i = 0; i < arr_size; i++) \ arr[i] = *parr++; } 

It is not safe (without border checking), it only overwrites sequential elements from the very beginning, but sometimes I find it useful.

Full example:

 #include <stdio.h> #define arr_set(type, arr, ...) { \ int arr_size = sizeof((type[]){__VA_ARGS__}) / sizeof(type); \ type *parr = (type[]){__VA_ARGS__}; \ for (int i = 0; i < arr_size; i++) \ arr[i] = *parr++; } #define print_buf_int(buf) { \ for (int i = 0; i < sizeof(buf)/sizeof(buf[0]); i++) \ printf("%d ", (int)buf[i]); \ printf("\n"); } #define print_buf_char(buf) { \ for (int i = 0; i < sizeof(buf)/sizeof(buf[0]); i++) \ if (buf[i]) \ printf("%c ", (char)buf[i]); \ else \ printf("null "); \ printf("\n"); } #define print_buf_str(buf) { \ for (int i = 0; i < sizeof(buf)/sizeof(buf[0]); i++) \ printf("%s ", (char*)buf[i]); \ printf("\n"); } int main() { int brr[8] = {}; int arr[8] = {3, 4, 5, 1, 0, 2}; print_buf_int(arr); arr_set(int, arr, 9, 8 ,7); print_buf_int(arr); arr_set(int, arr, 0, 1, 2, 3, 4, 5, 6, 7); print_buf_int(arr); arr_set(int, arr, 11, 22, 33, 44, 55, 66, 77, 88, 99); // 'brr' gets overwritten!!! print_buf_int(arr); print_buf_int(brr); unsigned char crr[13] = {'a', 'b', 'c', [12] = 'z'}; print_buf_char(crr); arr_set(char, crr, 'd', 'e', 'f', '1', '2', '3'); print_buf_char(crr); unsigned short srr[] = {111, 222, 333, 444, 555}; print_buf_int(srr); arr_set(unsigned short, srr, -1, 0, 666); print_buf_int(srr); char *prr[] = {"hello", "variadic", "world"}; print_buf_str(prr); arr_set(char*, prr, "good", "bye"); print_buf_str(prr); } 

Conclusion:

 3 4 5 1 0 2 0 0 9 8 7 1 0 2 0 0 0 1 2 3 4 5 6 7 11 22 33 44 55 66 77 88 99 0 0 0 0 0 0 0 abc null null null null null null null null null zdef 1 2 3 null null null null null null z 111 222 333 444 555 65535 0 666 444 555 hello variadic world good bye world 
0
source

Source: https://habr.com/ru/post/1402362/


All Articles