1

I'm trying to store a map of addresses in an array.

The following code snippet works as expected on my STM32F767ZI, but compiles with a warning...

intptr_t addressMap[2];

int* a=NULL;
int* b=NULL;

*a=10;
*b=20;

addressMap[0]=(intptr_t) a;
addressMap[1]=(intptr_t) b;

int* c=addressMap[0];

compiles with a warning:

initialization makes pointer from integer without a cast [-Wint-conversion]

at the last line (int* c=addressMap[0];).

I also tried uint32_t and int32_t as the data type of the addressMap array. Same warning.

According to this document (http://www.keil.com/support/man/docs/armcc/armcc_chr1359125009502.htm) addresses are 32 bit wide (as expected).

How may I write my code without this warning?

Johann Horvat
  • 1,285
  • 1
  • 14
  • 18

1 Answers1

3

How may I write my code without this warning?

as the warning says just add a cast doing

int* c = (int*) addressMap[0];

to avoid the warning initialization makes pointer from integer without a cast [-Wint-conversion]

But, I recommend you to not use intptr_t but directely int* if the goal of addressMap is to contains pointers to int , thanks to that you do not need cast at all :

int * addressMap[2];

int* a=NULL;
int* b=NULL;

*a=10;
*b=20;

addressMap[0] = a;
addressMap[1] = b;

int* c = addressMap[0];
bruno
  • 32,421
  • 7
  • 25
  • 37
  • 2
    @JohannHorvat you welcome. The less you have cast in code and warnings because of the code, the better it is. I encourage you to compile asking the compiler to produce the max warning level, for instance using _gcc_ do `gcc -pedantic -Wall -Wextra ...` – bruno Apr 24 '19 at 21:39