Ways to a declare a pointer? - c++

Why does the following code compile:
int main()
{
int j = 1;
int *jp = &j;
cout << "j is " << j << endl;
cout << "jp is " << jp << endl;
cout << "*jp is " << *jp << endl;
cout << "&j is " << &j << endl;
cout << "&jp is " << &jp << endl;
}
but not this?
#include <iostream>
using namespace std;
int main()
{
int j = 1;
int *jp;
*jp =& j; // This is the only change I have made.
cout << "j is " << j << endl;
cout << "jp is " << jp << endl;
cout << "*jp is " << *jp << endl;
cout << "&j is " << &j << endl;
cout << "&jp is " << &jp << endl;
}
This compiles when I do jp = &j, why? I have only initialized jp in another line, this is not making sense to me.

int *jp;
jp is a pointer. Its value (jp) is a memory address. It points to (*jp) an integer. When you do
jp = &j;
This sets the value to the memory address of j. So now *jp will point to j. When you do
*jp = &j;
This sets the value of the thing jp is pointing to to the memory address of j. When you do:
int *jp;
*jp = &j;
jp is not pointing to anything yet - its value is uninitialized. *jp = &j tries to follow the memory address of the value of jp, which is something random, and set it to &j... which will probably cause a segfault.
To clarify: The * in (int *jp;) is a different one than in *jp = .... The former just declares jp as a pointer. The latter defines how you do the assignment. To make it even more explicit, doing:
int *jp = &j;
is the same as
int *jp; jp = &j;
Note there is no * on the assignment.

*jp = &j;
This right here. Take note of the *. This goes and gets the data pointed to by the pointer, similar to what -> does for objects. What you're doing here is assigning a memory address to an int. The pointer jp points to an int called *jp.
In short, jp is a pointer to an int (therefore an address) and *jp is the int that jp points to. Using &j is getting you the address of j, or a pointer to it.

Change this:
*jp =& j; //this is the only change I have made,
Into this:
jp =& j;
That will make it compile.
The reason int *jp = &j; compiles is because it is declaring and initializing a value. In the non-compiling code, you are doing an assignment. And trying to assign an integer expression (*jp) to a pointer one (&j). Which doesn't make sense at all.
If you do:
jp = &j;
Then it will compile because you are assigning a pointer expression (&j) into a pointer variable (jp). There is type concordance in this case, so it makes sense it is compiling.

*jp =& j; says "write the value of &j to the location that jp points to". This is wrong a) because jp doesn't point to anything and b) because &j is a pointer and the value that jp points to should be an int, not a pointer.

int *jp = &j; declares jp to be an int* (pointer to int), and initialises it with &j (the address of j).
*jp = &j dereferences the pointer to j (giving a reference to j), and then attempts to assign &j to j. This is illegal because j is an int and &j is an int*.

You're parsing it in your head wrong.
The statement:
int *jp = &j;
really ought to be read as:
int* jp = &j;
Resulting in the actins:
declare jp as a pointer to an integer
assign the value of &j to jp
which matches the correction of your second code block:
int *jp;
jp = &j; // This is the only change I have made.

You're getting tripped up by the difference between an initialization and an assignment; the two are different things.
The line
int *jp = &j;
declares jp as a pointer to int and initializes it with the result of the expression &j, which has type "pointer to int" (int *).
The line
*jp = &j;
attempts to assign the result of the expression &j (which has type int *, remember) to the result of the expression *jp (which has the type int1). Since the types of the two expressions don't match, you get a compile-time error.
Note that the fact that jp hasn't been initialized to point anywhere meaningful yet would be a runtime error, not a compile-time error; IOW, if you had written *jp = 5; instead, the program would have compiled just fine (the expressions *jp and 5 have compatible types), but would have (most likely) blown up when you tried to run it.
1 Declarations in C and C++ are based on the types of expressions, not objects. For example, we're declaring jp as a pointer to int. To retrieve the value of the integer being pointed to, we dereference the pointer with the * operator, as in the statement
x = *jp;
The type of the expression *jp is int, so the declaration of the pointer is
int *jp;

Related

Pointers and references

I am currently learning c++ and pointers inside of c++. I am curious as to why I get a different memory address when I reference a pointer and why it isn't the same as the reference to the variable. Here is my super simple code:
#include <iostream>
using namespace std;
int main() {
int n = 50;
int *p = &n;
cout << "Number: " << n << endl;
cout << "Number with &: " << &n << endl;
cout << "Pointer: " << p << endl;
cout << "Pointer with *: " << *p << endl;
cout << "Pointer with &: " << &p << endl;
}
Why does &p give me a different address than "&n" and "p"? Thank you
First you declare an int variable n with the value of 50. This value will be stored at an address in memory. When you print n you get the value that the variable n holds.
Then you declare a pointer *p pointing to the variable n. This is a new variable that will hold the address to the variable n, where it is stored in memory. When you print p you will get back that address.
You can also ask for the address to where the value of n is stored by using the & symbol. This is what you get when you print &n, this will be the same as the pointer p.
Using the * symbol before a pointer will dereference it, meaning instead of returning the address that is the pointer, it will return the value that the pointer is pointing to. In your case this is an int with the value 50.
Lastly, the pointer *p that you declared is stored at it's own address in memory. Just as you could get the address to the variable n with the & symbol, you can get the address of p by using &p. This will be different from the address of n since it's not where 50 is stored, it's where the pointer to the value 50 is stored.

Pointer Syntax Within Condition

After a pointer is initialized, do you have to use the * dereference operator to call the pointer in a condition?
Example:
int main()
{
int var = 10;
int *ptr = &var;
if(ptr) // does this need to be if(*ptr) ???
{.......}
}
And can I have a short explanation as to why?
Thank you.
if (ptr)
check if the pointer is not Null but
if (*ptr)
check if the value it points to is not zero (in this example is 10)
So for checking the value you shoud add *.
It depends on what you want to do.
if(ptr) checks if the pointer value is nullptr or not. Note that this is shorthand for if(ptr != nullptr).
if(*ptr) checks if what the pointer points to is nullptr (or 0) - and in that case, since you dereference (follow) the pointer to answer the question, the pointer itself had better not be nullptr in that case.
First of all, a pointer is only a variable. However, there are different contexts in which you can use it.
As any other variable you can access the pointers content (which is the adress of the underlying memory) as follows:
int i = 1;
int * p = &i;
std::cout << p << std::endl
this would output the adress of i since this is what is stored in p
If you however want to access the content of the underlying memory (the value of i), you need to dereference the pointer first by using the * operator:
std::cout << *p << std::endl;
This would print the value of iso 1.
of course you can also access the pointer's adress (since the adress of i is a numeric value as well and needs to be stored somewhere too):
std::cout << &p << std::endl;
That would output the adress of p so the adress where the adress of i is stored :)
As a little example try to run this code:
#include <iostream>
int main() {
int i = 1;
int * p = &i;
std::cout << "Value of i(i): " << i << std::endl
<< "Adress of i(&i): " << &i << std::endl
<< "Value of p(p): " << p << std::endl
<< "Dereferenced p(*p): " << *p << std::endl
<< "Adress of p(&p): " << &p << std::endl
<< "Dereferenced adress of p(*(&p)): " << *(&p) << std::endl;
}

Pointer to a given memory address -C++

basic syntax of pointers: *ptr= &a
here &a will return the memory address of variable a and *ptr will store value of variable a
I want to ask, is it possible to make a pointer return a value from a given memory address? if yes what's the syntax
Yes, you can construct a pointer that refers to some arbitrary address in memory, by initialising the pointer with the address directly, instead of with an expression like &a:
int* ptr = (int*)0x1234ABCD; // hex for convenience
std::cout << *ptr;
Be careful, though, as it is rare that you know such a memory address exactly.
The cast (int*) is required as no implicit conversion exists between int and int*.
Yes. Use the dereference operator *.
For example;
int * a = new int;
int * b = new int;
*a = 5;
// Now a points to a memory location where the number 5 is stored.
b = a; //b now points to the same memory location.
cout << *b << endl; ///prints 5.
cout << a << " " << b << endl; //prints the same address.
int * c = new int;
c = *a;
// Now c points to another memory location than a, but the value is the same.
cout << *c << endl; ///prints 5.
cout << a << " " << c << endl; //prints different addresses.

How to get address of a pointer in c/c++?

How to get address of a pointer in c/c++?
Eg: I have below code.
int a =10;
int *p = &a;
So how do I get address of pointer p?
Now I want to print address of p, what should I do?
print("%s",???) what I pass to ???.
To get the address of p do:
int **pp = &p;
and you can go on:
int ***ppp = &pp;
int ****pppp = &ppp;
...
or, only in C++11, you can do:
auto pp = std::addressof(p);
To print the address in C, most compilers support %p, so you can simply do:
printf("addr: %p", pp);
otherwise you need to cast it (assuming a 32 bit platform)
printf("addr: 0x%u", (unsigned)pp);
In C++ you can do:
cout << "addr: " << pp;
int a = 10;
To get the address of a, you do: &a (address of a) which returns an int* (pointer to int)
int *p = &a;
Then you store the address of a in p which is of type int*.
Finally, if you do &p you get the address of p which is of type int**, i.e. pointer to pointer to int:
int** p_ptr = &p;
just seen your edit:
to print out the pointer's address, you either need to convert it:
printf("address of pointer is: 0x%0X\n", (unsigned)&p);
printf("address of pointer to pointer is: 0x%0X\n", (unsigned)&p_ptr);
or if your printf supports it, use the %p:
printf("address of pointer is: %p\n", p);
printf("address of pointer to pointer is: %p\n", p_ptr);
&a gives address of a - &p gives address of p.
int * * p_to_p = &p;
Having this C source:
int a = 10;
int * ptr = &a;
Use this
printf("The address of ptr is %p\n", (void *) &ptr);
to print the address of ptr.
Please note that the conversion specifier p is the only conversion specifier to print a pointer's value and it is defined to be used with void* typed pointers only.
From man printf:
p
The void * pointer argument is printed in hexadecimal (as if
by %#x or %#lx).
You can use the %p formatter. It's always best practice cast your pointer void* before printing.
The C standard says:
The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.
Here's how you do it:
printf("%p", (void*)p);
You can use %p in C
In C:
printf("%p",p)
In C++:
cout<<"Address of pointer p is: "<<p
you can use this
in C
int a =10;
int *p = &a;
int **pp = &p;
printf("%u",&p);
in C++
cout<<p;
In C++ you can do:
// Declaration and assign variable a
int a = 7;
// Declaration pointer b
int* b;
// Assign address of variable a to pointer b
b = &a;
// Declaration pointer c
int** c;
// Assign address of pointer b to pointer c
c = &b;
std::cout << "a: " << a << "\n"; // Print value of variable a
std::cout << "&a: " << &a << "\n"; // Print address of variable a
std::cout << "" << "" << "\n";
std::cout << "b: " << b << "\n"; // Print address of variable a
std::cout << "*b: " << *b << "\n"; // Print value of variable a
std::cout << "&b: " << &b << "\n"; // Print address of pointer b
std::cout << "" << "" << "\n";
std::cout << "c: " << c << "\n"; // Print address of pointer b
std::cout << "**c: " << **c << "\n"; // Print value of variable a
std::cout << "*c: " << *c << "\n"; // Print address of variable a
std::cout << "&c: " << &c << "\n"; // Print address of pointer c
First, you should understand the pointer is not complex. A pointer is showing the address of the variable.
Example:
int a = 10;
int *p = &a; // This means giving a pointer of variable "a" to int pointer variable "p"
And, you should understand "Pointer is an address" and "address is numerical value". So, you can get the address of variable as Integer.
int a = 10;
unsigned long address = (unsigned long)&a;
// comparison
printf("%p\n", &a);
printf("%ld\n", address);
output is below
0x7fff1216619c
7fff1216619c
Note:
If you use a 64-bit computer, you can't get pointer by the way below.
int a = 10;
unsigned int address = (unsigned int)&a;
Because pointer is 8 bytes (64 bit) on a 64-bit machine, but int is 4 bytes. So, you can't give an 8-byte memory address to 4 bytes variable.
You have to use long long or long to get an address of the variable.
long long is always 8 bytes.
long is 4 bytes when code was compiled for a 32-bit machine.
long is 8 bytes when code was compiled for a 64-bit machine.
Therefore, you should use long to receive a pointer.
If you are trying to compile these codes from a Linux terminal, you might get an error saying
expects argument type int
Its because, when you try to get the memory address by printf, you cannot specify it as %d as its shown in the video.
Instead of that try to put %p.
Example:
// this might works fine since the out put is an integer as its expected.
printf("%d\n", *p);
// but to get the address:
printf("%p\n", p);
Building off the above answers, if you had the pointer as a private variable, then you can make a getter function like so:
private int* p;
public int** GetP() { return &p; }
Then when you use it you create a pointer to your class that contains it:
DummyClass* p_dummyClass = new DummyClass;
And then in your use case scenario:
p_dummyClass -> GetP();
(don't forget to deallocate)
static inline unsigned long get_address(void *input){
unsigned long ret;
__asm__(
".intel_syntax noprefix;"
"mov rax, %1;"
"mov %0, rax;"
".att_syntax;"
: "=r"(ret)
: "r"(input));
return ret;
}

integer pointer to constant integer

I'd like to know what is happening internally and its relation to values displayed.
The code is:
# include <iostream>
int main(){
using namespace std;
const int a = 10;
int* p = &a; //When compiling it generates warning "initialization from int* to
//const int* discard const -- but no error is generated
cout << &a <<"\t" << p <<endl; //output: 0x246ff08 0x246ff08 (same values)
cout << a << "\t" << *p << endl; //output: 10 10
//Now..
*p = 11;
cout << &a <<"\t" << p <<endl; //output: 0x246ff08 0x246ff08 (essentially,
//same values and same as above, but..)
cout << a << "\t" << *p << endl; //output: 10 11
return 0;
}
QUESTION: If p = address-of-a, how come a=10, but *p = (goto address of a and read value in the memory location) = 11?
cout << a << "\t" << *p << endl; //output: 10 11
You lied to the compiler and it got its revenge.
With:
const int a = 10;
you promised you'll never modify a object.
You promised not to modify a, and the compiler believed you. So it decided to optimise away the reading of a, because it trusted you. You broke your promise.
Having said that, a real C++ compiler won't compile your code because int* p = &a is illegal. So perhaps you are lying to us as well. Or perhaps you need to get a real C++ compiler.
Just like Ouah said compiler has taken its revenge
but I disagree with you that it doesn't give any error
const int a = 10;
int* p = &a;
You cannot assign constant to a non constant pointer.
In C, this is undefined behavior. In C++, this won't compile. It's been repeated by David Heffernan multiple times but it is worth repeating. Here is the error you should get on a modern compiler:
main.cpp:7:10: error: cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'
int* p = &a; //When compiling it generates warning "initialization from int* to