+ 1

what is the difference between *ptr and int * ptr

pointers

3rd Aug 2017, 8:56 AM
Eshaan Belani
4 odpowiedzi
+ 11
When you do: int* ptr; You are declaring a pointer of type integer named ptr. When you do: *ptr; You tell the program to point to the address stored in ptr. E.g. int x = 5; int *ptr = &x; // ptr stores address of x. cout << *ptr; // point to the address of x, and print the value.
3rd Aug 2017, 9:06 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
@Fabian has also made legit points.
3rd Aug 2017, 9:11 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
there is no difference between int* ptr and int *ptr. but consider you try to create two pointers in a line: int *x, *y; // creates two int pointers int* x, y; //creates an int pointer x but y is "just" an int. Therefore int *x usually results in less errors. Edit: reading the other reply I might have misunderstood your question.
3rd Aug 2017, 9:04 AM
Fabian Ess
Fabian Ess - avatar
0
thanks everyone for explaining it so clearly
3rd Aug 2017, 10:26 AM
Eshaan Belani