4

I have a question:

const int a = 10;
int *ptr;
ptr = (int *)&a;

What is the use of (int *) in the third line above?

Just like the above if we have:

char str[] = "abc";
char *pc;
pc = str;

Is the above assignment correct?

And if it is, then why is it not in the first case?

0

4 Answers 4

3
const int a = 10;

a is stored in read-only area (basically to tell compiler that this value cannot be changed).

a is of type const int, and &a is of type const int *.

So type casting is need to match it:

int *ptr = (int *)&a;

You can avoid it by declaring as below:

int const *ptr = &a;

p is of type const int, types are matching, no issue.


updated for your query:

Pointer to constant can be declared in the following two ways.

  1. const int *ptr;

  2. int const *ptr;

Both are same. In this case we can change pointer to point to any other integer variable, but cannot change value of object (entity) pointed using pointer ptr.

But this is not the same as 1st: int *const ptr; — this is a Constant pointer to a variable.
In this case we can change value of object pointed by pointer, but cannot change the pointer to point another variable.

Sign up to request clarification or add additional context in comments.

2 Comments

if we do not want it to typecast we have to declare it 1)int const *ptr or 2) int *const ptr or both are same thing
@user3794823 No both are different see above for updated answer
3

"const int a" is a constant integer. It cannot be modified. If you try to assign to it, the compiler won't let you. If you successfully play tricks to get around the compiler, your program might crash or worse.

"int* ptr" is a pointer to non-constant integers. If you write an assignment "*ptr = 100;" the compiler will allow it without any problems. If ptr is a null pointer or undefined, this will crash or worse. If ptr points to non-constant integer, it will assign 100 to that integer. If ptr points to a constant integer, it will crash or worse.

Because of that, the compiler doesn't allow you to assign the address of a to p. Because it is asking for trouble.

Now the assignment "ptr = (int *)&a;". &a is the address of a. a is a constant integer, &a is a pointer to a constant integer. (int *) is a cast. You tell the compiler to convert &a to a "pointer to int". Because it is a pointer to int, not pointer to const int, you can assign this to ptr. But the thing that ptr points to is still a and it is still constant.

If you read *ptr, this will work just fine. Reading *ptr will give the number 10. Writing to *ptr, like *ptr = 100, will crash or worse. Basically, by using the cast you told the compiler "shut up, I know what I'm doing". You better do.

Comments

2

Without that cast you'll get a compiler warning. &a is a const int * not a int *.

Comments

-1

You need the cast because you forgot to declare ptr as a pointer to a constant int:

int const *ptr;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.