Author Topic: [cpctelera] warning 196: pointer target lost const qualifier  (Read 700 times)

0 Members and 1 Guest are viewing this topic.

Offline cperezgrin

  • CPC464
  • **
  • Posts: 30
  • Country: es
  • Liked: 15
  • Likes Given: 5
Is that warning something normal or do I need to check something?Also, is there any limitation how large a C file must be?I'm having some abnormal execution when I simply add lines like "if (shoot == 1) ..." even when that part is never executed. When I remove it, everything seems ok.

The warning results for the following line:
objetos[0].sprite = G_Nave;
where:
typedef struct {
    u8 x, y, z;
    u16* sprite;
    u8 alto;
    u8 ancho;
    int offset;
    u16* sprite_otro;
    u8 alto_otro;
    u8 ancho_otro;
    int offset_otro;
} TObjetos;
TObjetos objetos[11];
const unsigned char G_Nave = {
    0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x03, 0x8E, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x0C, 0x01, 0x80, 0x00, 0x00,
    0x00, 0x00, 0x0C, 0x01, 0x80, 0x00, 0x00, ....}

Thanks.

Offline Docent

  • CPC6128
  • ****
  • Posts: 166
  • Country: pl
  • Liked: 108
  • Likes Given: 0
Re: [cpctelera] warning 196: pointer target lost const qualifier
« Reply #1 on: 20:30, 10 September 18 »
Is that warning something normal or do I need to check something?Also, is there any limitation how large a C file must be?I'm having some abnormal execution when I simply add lines like "if (shoot == 1) ..." even when that part is never executed. When I remove it, everything seems ok.

The warning results for the following line:
objetos[0].sprite = G_Nave;
where:
typedef struct {
    u8 x, y, z;
    u16* sprite;
    u8 alto;
    u8 ancho;
    int offset;
    u16* sprite_otro;
    u8 alto_otro;
    u8 ancho_otro;
    int offset_otro;
} TObjetos;
TObjetos objetos[11];
const unsigned char G_Nave = {
    0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x03, 0x8E, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x0C, 0x01, 0x80, 0x00, 0x00,
    0x00, 0x00, 0x0C, 0x01, 0x80, 0x00, 0x00, ....}

Thanks.

use
objetos[0].sprite=&G_Nave;
instead.

Online robcfg

  • Supporter
  • 6128 Plus
  • *
  • Posts: 2.380
  • Country: se
  • 8-Bit Technomancer
    • index.php?action=treasury
  • Liked: 1072
  • Likes Given: 2603
Re: [cpctelera] warning 196: pointer target lost const qualifier
« Reply #2 on: 20:56, 10 September 18 »
There are several issues here.


1) G_Nave should be declared as "const unsigned char[] G_Nave"
2) You are assigning G_Nave, which is an array of bytes, to a word pointer. If that's the intention, you should use a cast like "objetos[0].sprite = (u16*)&G_Nave[0];"
3) Why is the sprite made of words? If you access the sprite data with the word pointer, it will skip every second byte of the sprite...
4) The sprite field should keep the const qualifier otherwise you are assigning something that you said you are not going to modify to a pointer that allows modifying. (const u16* sprite;)


Regarding the size question, how big is your file?

Offline cperezgrin

  • CPC464
  • **
  • Posts: 30
  • Country: es
  • Liked: 15
  • Likes Given: 5
Re: [cpctelera] warning 196: pointer target lost const qualifier
« Reply #3 on: 21:15, 10 September 18 »
Sorry about the code, I copied an old one, the correct one is:
Code: [Select]
typedef struct {
    u8 x;
    u8 y;
    u8 z;
    u8* sprite;
    u8 alto;
    u8 ancho;
    i8 offset;
    u8* sprite_otro;
    u8 alto_otro;
    u8 ancho_otro;
    i8 offset_otro;
} TObjetos;
const unsigned char G_Nave [238] = {
    0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x03, 0x8E, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x0C, 0x01, 0x80, 0x00, 0x00,
    0x00, 0x00, 0x0C, 0x01, 0x80, 0x00, 0x00, ....}
I want to create a struct so I can create objects in objetos[] that can change the sprite:

if (A) objetos[0].sprite = G_Nave;
else objetos[0].sprite = G_Nave2;

Do I have to change     u8* sprite; to    const u8* sprite; ?

The C file is now 18KB.

I really need to refresh C language, I was always afraid of pointers
« Last Edit: 21:21, 10 September 18 by cperezgrin »

Online robcfg

  • Supporter
  • 6128 Plus
  • *
  • Posts: 2.380
  • Country: se
  • 8-Bit Technomancer
    • index.php?action=treasury
  • Liked: 1072
  • Likes Given: 2603
Re: [cpctelera] warning 196: pointer target lost const qualifier
« Reply #4 on: 21:57, 10 September 18 »
In C you can have a pointer to a constant and a constant pointer.


const u8* sprite; means a pointer to a constant u8. So, you can modify the pointer itself, but not the value it's pointing to.


On the other hand, u8* const sprite = &G_Nave[0]; would create a pointer to G_Nave that you cannot modify. It will always point to the beginning of G_Nave.


So, the warning means that you are telling sprite to point to constant data while the pointer allows you to modify it. The right thing to do here is use const u8* sprite; which will let you assign the pointers but will not allow you to change data declared as constant.


Hope this helps!
« Last Edit: 21:59, 10 September 18 by robcfg »

Offline cperezgrin

  • CPC464
  • **
  • Posts: 30
  • Country: es
  • Liked: 15
  • Likes Given: 5
Re: [cpctelera] warning 196: pointer target lost const qualifier
« Reply #5 on: 22:08, 10 September 18 »
It helps indeed. That is what I want to do but didn't know how to.
Also, all the abnormal execution seems to be gone. So, as I expected, it was caused by my bad programming and now I can go on.
Thanks for all your collaboration and apologies for my mistakes. :doh:

Online robcfg

  • Supporter
  • 6128 Plus
  • *
  • Posts: 2.380
  • Country: se
  • 8-Bit Technomancer
    • index.php?action=treasury
  • Liked: 1072
  • Likes Given: 2603
Re: [cpctelera] warning 196: pointer target lost const qualifier
« Reply #6 on: 22:40, 10 September 18 »
I'm glad you got it working!


Don't hesitate to ask anything and remember that making mistakes is the way to learn new things.