Kay
Kay

Reputation: 131

C pointer to a const - how to quieten warnings

I have an array like this:

const uint8_t Translations[] =
{
    0xA6,0xA0,1,0,
    0xA1,0x87,2,0,
    0xA0,0xBE,3,0,
    0,
};

and I would like to access it with a pointer:

uint8_t * p_translations = Translations;    

I get the warning:

warning: assigning to 'uint8_t *' from 'const uint8_t *const' discards qualifiers.

I'm fine with that as I only read from and never write to *p_translations.

Is there a way to quieten the compiler (Microchip XC8) without suppressing warnings?

Upvotes: 1

Views: 703

Answers (3)

chqrlie
chqrlie

Reputation: 144989

You have a warning because you store the address of a const array into a pointer that is not const qualified.

Use const uint8_t *p_translations = Translations; to fix the problem.

Note that const uint8_t *p_translations is equivalent to uint8_t const *p_translations, but different from uint8_t * const p_translations, which is a pointer that cannot be changed and points to data that can be changed through the pointer.

Defining p_translations as const uint8_t *p_translations is a way to tell the compiler that you do not intend to modify the data pointed to by p_translations using this pointer. Since Translations is itself defined as const data, it should only be manipulated through a const qualified pointer to detect code that would try and modify it.

Upvotes: 0

tas_dogu
tas_dogu

Reputation: 9

You should make your pointer const too, const with pointers mean that the memory region they are pointing to is read-only. That tells compiler that it can do or not do some stuff. In your case(not using const with pointer) compiler understands that as a regular pointer accessing a read-only memory part, and raises a warning, the reason for the warning is you can actually change the const variables using pointers, but you should avoid it at all costs because it's UB and your program might do stuff that it shouldn't. But when you declare your pointer as const it tells compiler to treat it like a const and doesn't allow any writing to that memory region. And keep in mind the declaration you use while creating pointer only tells the compiler what type of data you're pointing to. So it doesn't mean your pointer is not changeable. If you want to make your pointer const you declare it like this type* const ptr. I hope i provided enough help.

Upvotes: 0

Zoso
Zoso

Reputation: 3465

There is usually never a good reason to ever discard the const qualifier, as the warning states. Some possible reasons in the wild are while passing to APIs that expect raw unqualified pointers. If the API doesn't need to change the data pointed to by the pointer, it should have a const in the declaration.

Also, compiler warnings are usually something to aid the developer and are mostly warnings that should be heeded to.

For your case too, there's no reason why p_translations should not be a uint8_t const* if you never intend to modify what p_translations points to. If on the other hand, you had to modify Translations, then you should drop the const altogether from Translations. Use const to indicate entities that shall remain unchanged throughout the program lifecycle. It's perfectly fine to have a non-const array that would be modified, else adopt const. Here is one of many links to read on why const can be helpful apart from indicating the const-ness of the data, namely possible opportunity for compiler optimizations and avoiding writing code that accidentally modifies the data.

Upvotes: 3

Related Questions