Reputation:
I've been looking a bit into pointers. I've found the small differences in declaring pointers like int* x
, or int *x
. However, I've always thought of *x
as the actual value as the pointed memory. So when I read int *x = &a
, I read it as "assign value at pointed address of x
to &a
", which is obviously not what is happening. The variable a
mem address is being assigned to pointer x
. How should I be reading it? Whenever I see *x
, I just think it's the literal value at pointed address by x
. Is it because in any pointer declaration, whatever value it is first initialized with it is always treated as the assigned mem address to point at?
Upvotes: 1
Views: 109
Reputation: 21
I've found the small differences in declaring pointers like int* x, or int *x.
These two are the exact same, the space does not make a difference.
A pointer is just an integer that stores a memory address, like a door number which points to a house.
The ampersand in front of an existing variable is basically asking the variable what is your memory address. eg:
int var =8;
int* ptr = &var;
Here I am taking the memory address of the variable var and assigning it to a new variable called ptr.
Now I'm not 100% sure what your question is but hopefully this may help a little.
EDIT: I would highly recommend watching this video series, It's super long but the first couple of videos explain pointers, references and operators very well, take notes if you can. The Cherno C++ Tutorial
Upvotes: 0
Reputation: 222744
When you see a declaration such as:
int *x = &a;
you should interpret it as:
int *x;
x = &a;
The declaration syntax is visually confusing. It is not intended that int *x = &a;
have an appearance of *x = &a;
. What happened historically is that we had simple assignments, like x = &a;
, and we also had declarations. In the C style of declarations, we show how we want to use a variable:
int x; // I want x to be an int. Therefore, x is an int.
int *x; // I want *x to be an int. Therefore, x is a pointer to an int.
int x(); // I want x() to be an int. Therefore, x is a function that returns an int.
int x[3]; // I want x[i] to be an int. Therefore, x is an array of int.
int *x[3]; // I want *x[i] to be an int. Therefore, x is an array of pointers to int.
Next, we want to combine both the declaration and the assignment—partly for brevity in the source code, and partly to emphasize that this is the initial value for an object, the value it has the moment it is defined. To do this, the declaration and the assignment were jammed together, resulting in:
int *x = &a;
This causes *x = &a
to appear in the statement, but it is not assigning &a
to *x
. It is initializing x
, not *x
. The *x
appears only because it is part of the declaration. I suppose this could have been made less confusing by separating the assignment part and the declaration part but keeping them in the same statement, perhaps something like:
int *x : x = &a;
But, regardless of what could have been done, int *x = &a;
is what we have.
Upvotes: 1
Reputation: 41625
Depending on the context, *x
is pronounced very differently.
int *x;
This is pronounced: declare x
to be a pointer to an int.
y = *x;
This is pronounced: take the value that x
points to, and copy it to y
.
Having these two entirely different pronunciations may seem strange at first. But there's a deeper meaning to it. The inventors of the C programming language wanted to allow an alternative pronunciation for the above declaration.
The alternative pronunciation for int *x;
is: declare a variable such that the type of the expression *x
is int
.
This alternative pronunciation rule also works for very complicated types, like functions returning functions of functions, therefore it is worth learning.
Upvotes: 0
Reputation: 238351
I've found the small differences in declaring pointers like int* x, or int *x
There is no semantic difference. Both are same as well as int*x
and int * x
.
How should I be reading [
int *x = &a
]?
"x
is initialised to point to a
". Or alternatively, "... to store the address of a
".
Furthermore, you should also read that x
was declared to have the type int*
i.e. pointer to int
. Depending on context, different aspects of a declaration may be more important than others. For example, if we already know that a
is an integer, it might be obvious from the context that a variable pointing to it is a pointer to an integer unless stated otherwise.
Upvotes: 1
Reputation: 505
"Assign the address of 'a' to 'x'".
If you make an integer as such called "sum":
#include <iostream>
int main(){
int sum = 57;
std::cout << &sum << std::endl; //Prints the address
std::cout << *(&sum) << std::endl; //Prints 57
std::cout << sum << std::endl; //Prints 57
return 0;
}
You will see that the ampersand is asking for the address of the variable. Pointers are just another variable, they contain addresses instead of what we would understand as "useful values".
As far as I'm aware, it's just a question of style("int *x" vs "int* x"). When you don't declare a variable, but you invoke it, then the asterisk acts as a dereference, i.e. it steps into the address contained within the variable to go back to whatever the pointer points to.
Upvotes: 0