In the above powerpoint slide, why is it that:
int* p = &x;
cout << *p << endl;
outputs 25? From the little understanding that I have with pointers, &x is the address of x, which is assigned to *p, the value of p. In that case, since &x is 0003, so shouldn't *p and its output also be 0003?
For all the advanced coders out there who cannot stand a simpleton like myself, I apologize in 'advance'.
&x is the address of x, which is assigned to *p
No, there is nothing assigned to *p.
In that line p is defined with type int* and p is assigned the address of x.
Now p holds the address 0003 and *p holds 25.
The * only relates to the variable definition in that line. During initialization you assign values to the variable you define.
If that wasn't the case, where should p point to in first place? Into which location should the 0003 be written then?
The slide is confusing, since it suggests that the name of the pointer variable were *p. The name of the variable is just p.
When you print the expression p, you will get 0003, since that is the value of the pointer.
So the pointer points to the object at address 0003, and the expression *p gets you the value from there, which is 25.
The slide should better say that cell 0001 has the name p. And that cell 0003 has two names (or paths to get there), which are x directly or *p indirectly.
When we declare pointer we use * with datatype for e.g int *p it doesn't mean that we are declaring as well as referring to value at the same time.
So when you type
int *p = &x;
it is similar as writing
int *p;
p = &x;
Now p is 0003 and value at address 0003 is 25. which you can get by *p.
The reference operator(&) refers to the address of variable x,which is assigned to p.Since p is holding the address of a memory location it is of pointer type (int*).Now the (*p) ,where (*) is the deference operator(*) refers to the value that the address in p is holding.
Related
int x = 2;
int y=8;
int* p = &x;
*p=y;
cout << p <<endl;
my question is: why do I get the memory adress when I Print p and not the actual value since I already dereferenced it in line 4
cout << *p << endl;
is what you need.
When you try to output something to stdout, compiler automatically infer its type and call the corresponding function. In your case, p is pointer type, therefore the address is printed, and since *p is int type, you should use *p if you want to print the value.
I think you have a misconception about dereferencing pointers.
...since I already dereferenced it in line 4
Dereferencing means to retrieve the value the pointer is pointing to. It doesn't change the pointer itself in any way. p is still a pointer after *p=y;.
All *p=y does, is to change the value p is pointing to, it doesn't change the pointer itself.
error: invalid conversion from 'int' to 'int*'
int *q = 8;
Works fine.
*q = 6;
Why can't I directly assign an int to an int pointer like this: int *q = 6; and I can assign it safely in the next line?
Because they're different things at all. The 1st one is definition of variable with initializer expression, i.e an initialization (of the pointer itself):
int * q = 8;
~~~~~ ~ ~~~
type is int*; name of variable is q; initialized with 8
The 2nd one is assignment (of the object pointed by the pointer):
*q = 6;
~~ ~~~
dereference on q via operator*; assign the resulting lvalue pointed by q to 6
And, int *p = 6; means define a variable named p with type int* and initialize it with 6, which fails because 6 can't be used for initializing a pointer directly (i.e. the error "invalid conversion from 'int' to 'int*'").
* symbol is reused for two different purpuses in your snippet. First time it is used as a part of type declaration int *, declaring a pointer to int. Second time it is used to dereference a pointer *q, invoking indirection operator.
* can also be used to invoke multiplication operator *q = *q * *q;;
To assign a value to an integer pointed by pointer you need to dereference it. And to assigning integral value other than 0 to a pointer itself (that is what int *q = 8; is doing) requires reinterpret_cast, hence you get this error.
Statement int *q defines a variable of type "pointer to integer", and an initialisation therefore needs to be a pointer value, not an integral value.
So int *q = 8 is not the same as int *q; *q = 8 (which would be undefined behaviour because it dereferences an uninitialized pointer), but more like int *q; q = 8, which makes the misunderstanding more transparent.
This:
int *q = 8;
is an initialization. It initializes q (the pointer), not *q (what it points to). Writing this equivalently with an assignment instead of initialization would look like:
int *q;
q = 8;
So you see it doesn't make sense. (and, isn't allowed of course -- an int is not a pointer)
Just to be sure, if you write:
int *q;
*q = 8;
this is syntactically correct, but undefined behavior. Your pointer doesn't point to an object of type int, it is uninitialized and probably pointing to some invalid location. Writing there, anything could happen.
Because the types don't match.
The 6 itself is not a value of pointer type, it's an integer so it cannot be directly stored in a pointer.
When you do *q = 6 the * dereferences the pointer, so the type becomes int (or rather an lvalue int, i.e. something that can be assigned to).
Here, assign the value 8 to an object of type int*. It means q points on address 8 on memory.
int *q = 8;
Is equivalent to:
int *q;
q = 8;
Is Not equivalent to:
int *q;
*q = 8;
is illegal, as it involves constraint violation.
Related stack overflow question: Is it possible to initialize a C pointer to NULL?
A pointer variable holds an address, or 'location' of something. Thus, the pointer holds a value that is a memory address. When you say:
int *q = 6;
you are creating a pointer variable that intends to point to an int, and telling it to point to the int value stored in the address 6, which is (probably) not what you really intended.
A pointer variable should point to some memory address that contains some actual data you want to access. For example:
int x = 5;
int *q = &x;
This creates a variable (x) that contains the value 5.
the next line creates a pointer variable that contains the address of x. You have set the pointer variable 'q' to the address of the int variable 'x'.
Now you can see what is at 'x' by doing this:
int y;
y = *q;
This says "take what is pointed to by q, and store it in y". The end effect is that y will be set to 5.
int x = 5; // create variable x and set it to 5
int *q = &x; // create int pointer variable, set it to the address of x
int y; // create variable y
y = *q; // take the value pointed to by q, and store it in y
If, for example the variable x is at memory location 1234, and you looked at the value stored in 'q', it would be 1234, which is the address of x.
When you say "y = *q", you are saying "take the value stored in the address 1234, and puts that value in y". Since the memory location 1234 is 'x', and 'x' was assigned the value 5, the value 5 will be what is stored at the address 1234.
y = *q;
will take the value stored in the address 1234 and assign y to that value, thus making y 5, which was the value stored in x, which is what q 'points' to.
This could be shortened to:
int x = 5;
int *q = &x;
int y = *q;
When you write
int *q = 8; it means you declared a pointer q and initialized a pointer with the integer 8. but q being a pointer expects a address value therefore you get error stating incompatibility.
Whereas when you write
*q=8 after declaring, it means you are dereferencing the addresses pointed by q and writing a value to that location. here q points to a int so you are writing 8 a integer value to the location pointed by q. that is right. This can also lead to error at runtime if q is not initialized to point to right location.
In your first statement you are declaring and initializing the pointer to some value of type int (on the same line). In your second statement you are changing the value pointed to by a pointer. Two different things. What you have there is initialization followed by an assignment. Don't let the * confuse you.
Because it is not valid C, simple as that. Specifically, it is a constraint violation of the assignment operator, since integer to pointer or pointer to integer is not valid forms of "simple assignment" (C11 6.5.16.1).
You can convert between integers and pointers by adding an explicit cast. The result is however not guaranteed to work: pointers and integers may have different representations and there might be alignment issues.
In case of *q = 6; you assign an int to an int, which is of course perfectly fine (given that the pointer points at allocated memory somewhere).
If you re-write it like this:
int* q = 8;
*q = 6;
Then you can see the * has two different purposes.
Try
int *q = new int(8);
But working with pointers here is not normally required. If you have to use pointer, use smart pointer shared_ptr<int> or uniqe_ptr<int>.
In this case you are assigning value 8 to pointer *q and it wont work during initialization, Memory address is unique at that time not available to assign but you can set once memory block created after initialization for *q.
I'm new to programming and I'm trying to wrap my head around the idea of 'pointers'.
int main()
{
int x = 5;
int *pointerToInteger = & x;
cout<<pointerToInteger;
}
Why is it that when I cout << pointerToInteger; the output is a hexdecimal value, BUT when I use cout << *pointerToInteger; the output is 5 ( x=5).
* has different meaning depending on the context.
Declaration of a pointer
int* ap; // It defines ap to be a pointer to an int.
void foo(int* p); // Declares function foo.
// foo expects a pointer to an int as an argument.
Dereference a pointer in an expression.
int i = 0;
int* ap = &i; // ap points to i
*ap = 10; // Indirectly sets the value of i to 10
A multiplication operator.
int i = 10*20; // Needs no explanation.
One way to look at it, is that the variable in your source/code, say
int a=0;
Makes the 'int a' refer to a value in memory, 0. If we make a new variable, this time a (potentially smaller) "int pointer", int *, and have it point to the &a (address of a)
int*p_a=&a; //(`p_a` meaning pointer to `a` see Hungarian notation)
Hungarian notation wiki
we get p_a that points to what the value &a is. Your talking about what is at the address of a now tho, and the *p_a is a pointer to whatever is at the &a (address of a).
This has uses when you want to modify a value in memory, without creating a duplicate container.
p_a itself has a footprint in memory however (potentially smaller than a itself) and when you cout<<p_a<<endl; you will write whatever the pointer address is, not whats there. *p_a however will be &a.
p_a is normally smaller than a itself, since its just a pointer to memory and not the value itself. Does that make sense? A vector of pointers will be easier to manage than a vector of values, but they will do the same thing in many regards.
If you declare a variable of some type, then you can also declare another variable pointing to it.
For example:
int a;
int* b = &a;
So in essence, for each basic type, we also have a corresponding pointer type.
For example: short and short*.
There are two ways to "look at" variable b (that's what probably confuses most beginners):
You can consider b as a variable of type int*.
You can consider *b as a variable of type int.
Hence, some people would declare int* b, whereas others would declare int *b.
But the fact of the matter is that these two declarations are identical (the spaces are meaningless).
You can use either b as a pointer to an integer value, or *b as the actual pointed integer value.
You can get (read) the pointed value: int c = *b.
And you can set (write) the pointed value: *b = 5.
A pointer can point to any memory address, and not only to the address of some variable that you have previously declared. However, you must be careful when using pointers in order to get or set the value located at the pointed memory address.
For example:
int* a = (int*)0x8000000;
Here, we have variable a pointing to memory address 0x8000000.
If this memory address is not mapped within the memory space of your program, then any read or write operation using *a will most likely cause your program to crash, due to a memory access violation.
You can safely change the value of a, but you should be very careful changing the value of *a.
yes the asterisk * have different meanings while declaring a pointer variable and while accessing data through pointer variable. for e.g
int input = 7;
int *i_ptr = &input;/*Here * indicates that i_ptr is a pointer variable
Also address is assigned to i_ptr, not to *iptr*/
cout<<*i_ptr;/* now this * is fetch the data from assigned address */
cout<<i_ptr;/*it prints address */
for e.g if you declare like int *ptr = 7; its wrong(not an error) as pointers ptr expects valid address but you provided constant(7). upto declaration it's okay but when you go for dereferencing it like *ptr it gives problem because it doesn't know what is that data/value at 7 location. So Its always advisable to assign pointers variable with valid addresses. for e.g
int input = 7;
int *i_ptr = &input;
cout<<*i_ptr;
for example
char *ptr = "Hello"; => here * is just to inform the compiler that ptr is a pointer variable not normal one &
Hello is a char array i.e valid address, so this syntax is okay. Now you can do
if(*ptr == 'H') {
/*....*/
}
else {
/*.... */
}
I have tried the output of the following code. But, i don't yet understand what might the value of q represent.
I understand that the *q points to p implying that, printing *q would print the address of p, where as **q would print the value at the address p points to, i.e., x(=5).
#include <iostream>
using namespace std;
int main()
{
int x=5;
int *p,**q;
p=&x;
q=&p;
cout<<q;
return 0;
}
So what does q alone represent? What is that value that is printed when just q is printed?
Just print value and ardess of your variable and you will see:
x: 5 &x: 0x7fff691dfcc4
p: 0x7fff691dfcc4 &p: 0x7fff691dfcb8
q: 0x7fff691dfcb8 &q: 0x7fff691dfcb0
&var - location;
var - value
You say:
printing *q would print the address of p
However this is not true. Printing q would print the address of p. After all, you assigned that in doing q = &p;.
Printing *q would print the value of p , which is the address of x.
Since q is a pointer to p, the value of q is the location of p, whatever that means on this particular platform. (Most likely the memory address containing p's value.)
(I will assume 64-bit pointers and 32-bit integers in this answer, just to have concrete values to write about. It holds with any vaues, though).
Let's analyse p first. p is 8 bytes storing an address (of the variable x). Printing *p will print the value residing at that address, which is the 4 bytes of the value of variable x. Printing just p will print the 8 bytes of the address stored in p.
Now, the same applies to q. q is 8 bytes storing an address of the variable p. So printing *p will print the value residing at that address, which is the 8 bytes of the value of p. Printing just q will print the 8 bytes of the address stored in q.
int x = 1;
A block of memory is allocated to hold int value. And that block is named as 'x'. Let's say x is allocated at 0x1234.
int *p;
Here 'p' is pointer to int that means p would contain the address of some int. Let's say p is allocated address 0x2345.
p = &x;
This would make p contain the address of x i.e 0x1234 would be stored in the location allotted to p.
int **q;
Here q is pointer to pointer to an int that means q would contain the address of pointer to int. Let's say q is allotted address 0x3456.
q = &p;
This would make q contain the address of p i.e 0x2345 would be stored in the location allotted to q.
Hope I am simple and clear...
Pointer is just a data type, being not very different from other types.
It stores an address of another data object, and you can use *p to access the data object.
So, pointers are not different from other types but their contents have some special meaning.
(int**) is such a pointer that points to a data object of type (int*), being not very different from other pointers (they may point to data object of type int).
So, the content of q is a pointer to p. *q indicates you use the content to find p.
the content of p is a pointer to an Integer, *p indicates you use the content to find the integer.
By adding ** before q in declaration of q, you are saying to compiler that, q is a pointer which will point to address of a pointer. So when you are printing q, you are actually printing address of p pointer, not the value of address p point to (i.e. x).
In your code, p is a pointer which is storing an address of an integer. But p itself has an address, which you are storing in q.
so, I've got my pointer:
int *p = new int(10);
And I print out the following things:
&p which is: 0xbdee018
p which is: 0xb8c254b0
&p stands for the address of the pointer, and p is stands for the address of the mapped value by the pointer itself.
Is that correct, or is it the very opposite?
Yes exactly. & gives the address of the element.
So
p gives the value currently inside the variable - p being a pointer, you get a pointer (to an int)
&p returns the address of p
Graphically
p value = 0xb8c254b0 --> int x 10
^
|-------- 0xbdee018
Yes you are right .
&p stand for address of pointer and p give address where it point.
p denotes the address of an integer, e.g. int*.
*p denotes the integer stored at p (dereferencing), e.g. int.
&p denotes the address of the pointer p. If p would be an int, you would obtain a pointer-to-int by referencing p, e.g. int*.
Yes you're totally correct.
&p is the the address of the pointer or you can say a pointer of your pointer.
p is the pointer of an array of 10 int elements.
I think you are getting thing in the correct way. A pointer has to be stored somewhere, so it has an address too. Try to see the memory with the debugger, by using the address you shown in your example, point to the address
0xbdee018
and check the bytes, you should see, in some orders the bytes:
0xb8c254b0
ie p.
The order of the bytes depends on the 'endianity' of the cpu you are using
Yes, it seems, that you got it right.
To ilustrate:
int i = 42;
int * pointer_to_i = &i;
Then:
i == 42 // obvious
&i == pointer_to_i // address, where a value 42 is stored
// for the sake of example, let's assume
// that &i == 0x12345678
&pointer_to_i // address, where a value 0x12345678 is stored
Additionally:
&<name> gets the address of a variable <name> (you can assign that to a pointer);
*<pointer> dereferences the <pointer>, that is retreives the actual data stored in a place pointed to by that pointer.
P is a pointer to int.(*p) means value at the pointer p which will be value of that int where p is pointing.(&p) will give you address of the pointer,means *(&p)=p.
I think you need to dereference the pointer to get at the value.
Try printing *p, as this prints the value stored at the address, this is a dereferenced pointer.
When you print &p, you are printing the address of the address of p.
Printing p prints the pointer to p.
Pointer is a variable that stores address of other variable that the pointer points to. This variable itself also has memory address, which is &p in this case if variable name is p. So:
"&p" is stands for the address of the pointer, and "p" is stands for the address of the mapped value by the pointer itself.
You are right. Besides, *p will get you the value of the object it points to. You may find Binky Pointer Fun Video useful.
In C++, many people prefer int* p over int *p. There is absolutely no difference between the two from a language point of view, it's just a matter of human perception.
int* p says: "p is a pointer to an integer".
&p is the memory location at which p is stored (ragardless of the data type of p).
p is the pointer itself. Its value is a memory location at which an integer ist stored (except if p == 0).
*p is (a reference to) the value that p points to.
In C, many people prefer int *p over int* p. It says: "*p is an integer". This has the advantage that you do not fall into traps like int* p, q, which declares an int* p and an int q.
For pointers (e.g., int* p):
p is the pointer (i.e., address of the pointed object)
&p is the address of the pointer.
*p is the pointed object (dereferencing).
For non pointers (e.g., int p):
p is the data
&p is its address