1

Im having a problem with the pointer to constant. Here are my code:

#include <stdio.h>

typedef struct _duong                   
{
    int tuoi;
    int chieucao;
}duong;             

struct _duong DUONG;

typedef struct _duong *pduong;

const pduong p;

int main(void)
{
    p = &DUONG;
    p->tuoi = 25;
    p->chieucao = 165;
    printf("tuoi : %d\n",DUONG.tuoi);
    printf("chieucao : %d\n",DUONG.chieucao);
    return 1;
}

compiler throw an error: Cannot assign to variable 'p' with const-qualified type 'const pduong'. I can see that the problem come from

const pduong p;

and

p = &DUONG;

for my understanding const pduong p is a pointer to constant so that we can not change the value of the pointed variable but we can change the address it point to. So can anyone help me why the compiler showed this error? Thank you.

Dương
  • 29
  • 3
  • 3
    Dont typedef pointers, my two minutes are gone.... – Sourav Ghosh Jul 26 '18 at 14:45
  • It is a constant pointer, not pointer to constant. – AnT stands with Russia Jul 26 '18 at 14:57
  • https://stackoverflow.com/questions/2253738/c-typedef-interpretation-of-const-pointers – AnT stands with Russia Jul 26 '18 at 14:59
  • i know the differences between the constant pointer and pointer to constant, but can you explain more specific for this case, why `const pduong p` here is a constant pointer? @AnT – Dương Jul 26 '18 at 15:43
  • @Dương: Follow the link above. Or do a search here. The question has been explained, explained and overexplained many times on SO. Honestly, I'd say that it is you who have to explain first why you expected it to be a pointer to constant. Why really? It is hard to answer your question without understanding the source of your confusion. – AnT stands with Russia Jul 26 '18 at 16:00

3 Answers3

2

No the pointer itself is a constant.

I.e you cannot change the pointer. But whatever that pointer points to you can change.

To correct this change

const pduong p; 

to

const pduong p = &DUONG;

And then drop the line

 p = &DUONG;

EDIT

Perhaps you want

const duong p_contents = {25, 165};
const pduong * const p = &p_contents;
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

And this is one of the reasons why I strongly recommend against typedef-ing pointer types - the const semantics apply to the pointer, not to what is being pointed to.

const pduong p;

means

struct _duong * const p; // p is a const pointer to non-const type

not

const struct _duong *p;  // p is a non-const pointer to const type

In other words, you are declaring p as a const pointer to struct _duong, not as a pointer to const struct _duong.

Personally, I would recommend you ditch the typedef altogether and declare the pointer explicitly:

const struct _duong *p;

Typedefs are great for abstracting away details, and typedef-ing pointer types is fine if the user doesn't have to be aware of the "pointer-ness" of the type. In this case, the user does have to be aware of p's "pointer-ness" to use it correctly, so its type should not be hidden behind a typedef.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

No, the address of a const pointer can't be changed. Only the value it is pointing to.

Jakob Herk
  • 157
  • 11