T. Noob
T. Noob

Reputation: 13

C "Error: Invalid initializer"

the following short code snippet results in a invalid initializer error, and as a beginner in C, I do not understand why.

unsigned char MES[] = { 0x00, .... };
unsigned char *in[] = &MES;

Is this not the correct way to do it?

Upvotes: 1

Views: 5229

Answers (2)

Riccardo Manfrin
Riccardo Manfrin

Reputation: 741

I think that what you are trying to achieve is the following:

unsigned char MES[] = { 0x00 };
unsigned char *in = MES;

qualifying in as an array (whose size is unknown) as follow

unsigned char (*in2)[] = &MES;

is not going to add valuable information to it aside that it has a finite size (which is true for any data) and if you print in and in2

printf ("%lx\n", (long unsigned int) in);
printf ("%lx\n", (long unsigned int) in2);

the value shall be the same.

Don't confuse the position of the data with the position of its reference.

Using &MES is like trying to read the position in memory where the position of the array is written. But this does not exist.

Consider the counterexample:

 void *reference_to_memoryarea = malloc(3);
 void **reference_to_the_reference = &reference_to_memoryarea;

Here the position exists and has a position in memory, where it is stored. And you can write into *reference_to_the_reference and generate a good leak

Upvotes: 0

Sander De Dycker
Sander De Dycker

Reputation: 16243

&MES is a pointer to an array of unsigned char.

in is an array of pointers to unsigned char.

Try instead :

unsigned char (*in)[] = &MES;

which makes in also a pointer to an array of unsigned char.

Upvotes: 1

Related Questions