0

l have a question about the pointer*

int a=1; int b=&a; or int a=1; int *b=&a; is it same?

6th Dec 2019, 9:59 AM
alicia
3 Answers
+ 3
alicia No ,they are different. ā—First: int a=1; int b=&a; This will result in error. b is an integer variable not a pointer to int. For assigning value to variables value must be compatible with type of variable. As there is no conversation possible from `int *` to `int` it'll give a compile time error. ā—Second int a=1; int *b=&a; You are assigning address of `a` to a pointer variable `b`. This is valid. Also there is no need of type conversion/casting in this case.
6th Dec 2019, 10:14 AM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 1
No, int b is a normal integer variable which has value equal to memory address of variable a. But int *b is a pointer variable that store the value of a not the location. If you print both b and *b then display some random integer(memory location) and 1 will show respectively. In simple form you can say that * is used for accessing value stored at a particular memory location.
6th Dec 2019, 10:18 AM
Vijay Meena
+ 1
Vijay Meena just adding, it's called dereferencing.
6th Dec 2019, 10:22 AM
Avinesh
Avinesh - avatar