int const *i Vs const int *i

Programming, for all ages and all languages.
Post Reply
Guest

int const *i Vs const int *i

Post by Guest »

Hello all,
Could you please tell me the difference between declaring i as "int const *i" and "const int *i"?

Any help greately appreciated.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:int const *i Vs const int *i

Post by Candy »

Guest wrote: Hello all,
Could you please tell me the difference between declaring i as "int const *i" and "const int *i"?

Any help greately appreciated.
One of them (my guess is const int *i) is a constant pointer to a variable i. You can modify what's there, but you can't make it point somewhere else (say, a hardware register). The other is a variable pointer (which can be set to some other place), but you can't modify what's there (say, as an index into a read-only array).
Guest

Re:int const *i Vs const int *i

Post by Guest »

Then, what about this set of code? The variable i gets pointed to the variable 'b'

Code: Select all

#include<stdio.h>
main()
{
        int a=1,b=2;
        const int *i=&a;
        i=&b;
        printf("%d\n",*i);
}
Legend

Re:int const *i Vs const int *i

Post by Legend »

Then it seems that you have the pointer to constant int case ...
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:int const *i Vs const int *i

Post by Candy »

as I'm guessing, it might also be that it doesn't matter whatsoever, both might be constant pointers to variable ints. It seems to me that there must be some way to make a variable pointer to constant ints...

after testing:

const int *a, variable pointer to constant int
int const *b, exactly the same
int *c const, gives error on compile, is nothing :)
int * const d, constant pointer to variable int

Thought the third was more likely than the last, but it is the last.
Guest

Re:int const *i Vs const int *i

Post by Guest »

Candy wrote: int * const d, constant pointer to variable int
int main()
{
int a=4, b=5;
int * const i=&a;
printf("A = %d",*i);
i=&b;
printf("\nB = %d",*i);
}

Though the compiler produces some warning while changing the address of 'i', the output shows that the value of 'i' can be changed?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:int const *i Vs const int *i

Post by Candy »

Guest wrote: Though the compiler produces some warning while changing the address of 'i', the output shows that the value of 'i' can be changed?
The entire point of const is to generate compiler warnings when you override it. I believe in C you can cast it to normal char * if you want to.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:int const *i Vs const int *i

Post by Neo »

IIRC The effect of the 'const' keyword is implementation-defined.
If you compile the above program with a C compiler it only gives you a warning but a C++ compiler gives an error.
The purpose of 'const' is to announce that objects may be placed in read-only memory and perhaps increase opportunities for optimization
HTH
Only Human
Post Reply