When you initialize an array, you can't say intarr[100] = {1, 1, 1, 1, 1...}
if there are more than 100 arrays, right? So if you give intarr[100] = {1};
, all of these have to be initialized to 1 in a one-dimensional array
In dev c++, only the value of 0 is initialized to 1. Then there's a way to initialize everything by using a for statement, but that's the way it used to work. Why isn't it working? (arr[100] = { -1,}
This is not allowed either.)
intarr[100] = {0};
says that all values are initialized to zero, but I think they thought this would be applied in all cases
to the above sites do you see down to
.int arr[100] = { [0 ... 99] = 1 };
I looked for more because they said no, but there was a way as below.
Similarly, this is the method shown in the link above.
#include <stdio.h>
#define x1 1
#define x2 x1, x1
#define x4 x2, x2
#define x8 x4, x4
#define x16 x8, x8
#define x32 x16, x16
#define x64 x32, x32
int main(){
int arr[100] = { x64, x32, x4 }; // 64 + 32 + 4 = 100
printf("%d\n", arr[99]); // Output: 1
return 0;
}
Even if x1 is not a value other than 1, it is all initialized to that value.
By the way, in the case of the code you mentioned above, it is possible to initialize it in the following way.
#include <stdio.h>
int main(){
int arr[100] = {[0 ... 49] = 3, [50 ... 99] = 7};
printf("%d\n", arr[49]); // Output: 3
printf("%d\n", arr[99]); // Output: 7
return 0;
}
Don't you usually use memset?
An array is a collection of data items having the same data type.
Initialization of Array at Compile Time and Run Time
https://theknowshares.com/computerscience/datastructure/arrays/
Initialization of Array at Compile Time:
int A[5] = {10,20,30,40,50};
Initialization of array at Run Time
//initialization of array
for(j=0; j<n; j++)
{
printf("\n enter element %d :",j);
scanf("%d",&a[j]);
}
© 2024 OneMinuteCode. All rights reserved.