0

I'm unsure how to fix the warning for this piece of code (warning pertains to the second line). This is not my code but it does work perfectly. However, I'd like to get rid of the warning but am confused. v is an unsigned long that we want to put to pointer. We are working with avr-gcc.

../../uJ/uj.c:1149:20: warning: initialization from incompatible pointer type const UInt8* d = &v;

static void ujThreadPrvPut32(UInt8* ptr, UInt32 v){         //to pointer
    const UInt8* d = &v;

    *ptr++ = *d++;
    *ptr++ = *d++;
    *ptr++ = *d++;
    *ptr = *d;
}
fluter
  • 13,238
  • 8
  • 62
  • 100
  • Pointers in C are to concrete type, i.e. d++ skip 1 byte, v argument is 4byte long. Compiler cannot understand what You want. Below is good answer with cast – Jacek Cz May 17 '16 at 12:23

2 Answers2

4

Add a cast:

const UInt8* d = (const UInt8*)&v;
atturri
  • 1,133
  • 7
  • 13
-1

As an addition to the other answer:

It would be better practice to supply the ptr argument as constant pointer in the first place.

static void ujThreadPrvPut32(UInt8* const ptr, UInt32 v);

Since ptr provides the target address where the data should go, this should not be changed by the function, only the CONTENTS at that location.

On the other hand, you could just use memcpy().

uint8_t* ptr = 0;   // has to be set to desired target address obviously
uint32_t value = 123;

memcpy(ptr, &value, sizeof(value));
Community
  • 1
  • 1
Rev
  • 5,827
  • 4
  • 27
  • 51