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.