"object of variable size may not be initialized"


here you see following listing in C :

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define addr 4194304

/* for-loop mit addr adresses */
int main(void)
{

int i;
int x;

for( i = 0; i<addr; i++ )
{
int x[i]=true;
if (i==addr)
break;else
continue; /* here should be a pointer with the aim for each address number to be set as true: int x[i]=true; */

}
return (0);
}

----------------------------------

yes, when I want to compile this, the error report of gcc in line 16 is:
"object of variable size may not be initialized"
it is probably not allowed to set an increasing index like i in x[i] as true.
means that then a pointer is necessary ...

how would be the solution for this ? I want that each counting step of i is set as true. Means each address should be set as true.

This programm would perform a platform-independent chip-update e.g. in bios on each mainboard.
No matter which manufactor and no matter which bios-chip.

I ask here, because I had the solution in 2005, but the solution got lost
because of new installation.

tuvm for reply.
cheers.
dschinn
int x[i]=true;

This is effectively *(x+i) = true; Given that neither x or i is a pointer, it's clearly nonsensical.

Did you mean x to be an array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE_OF_ARRAY 4194304

#define addr 4194304

/* for-loop mit addr adresses */
int main(void)
{

int i;
int x[SIZE_OF_ARRAY];

for( i = 0; i<addr; i++ )
{
int x[i]=true;

}
return (0);
}


This is a huge array. Did you really mean the array to be of size 4194304?
Last edited on
it says the error is on line 16 which is int x [i] equal true. i think you need to make it a 0
it says the error is on line 16 which is int x [i] equal true. i think you need to make it a 0


x[i] does not exist. Trying to set it to zero won't help. No such variable exists, because x is not an array.

hi you both !

thank you for your answer.

the concerning object x[i] should look like this: i counts from 0 to upper limit and each counted number has an x in the beginning.

Okay, I now check out your suggestions.

See you soon.

Regards.
dschinn

duh i figured it out you have int x twice which would make it a redifinition
dschinn wrote:
int x[i]=true;

...doesn't make sense. First, x is declared as an array of a non-constant size, which is a compilation error in itself. Second, the assignment shouldn't be allowed, unless the braces are used.

@Aramil of Elixia: The compiler will assume the most recently declared x, which is the x declared in the loop.

Wazzak
Last edited on
Perhaps you should read Moschops' advice. All that other stuff is nonsense.
First, x is declared as an array of a non-constant size, which is a compilation error in itself

not in C, which this question is about.

However, the question is too unclear to have a good answer.
guys: he redeclared x so if on line 16 he just had x[i] = true then it would be fine because it was given the value x[size_of_array] or in other words 4194304 so the redefinition of x with a non initialized int is what caused the error.
Topic archived. No new replies allowed.