pointer to pointer address - c

I have a question about pointer to pointer.
Here's my code
#include <stdio.h>
void main()
{
int num=10;
int *numPtr1;
int **numPtr2;
numPtr1 = &num;
numPtr2 = &numPtr1;
printf("%d\n", num);
printf("%d\n", *numPtr1);
printf("%d\n", **numPtr2);
printf("%p\n", &num);
printf("%p\n", numPtr1);
printf("%p", numPtr2);
}
Why numPtr2's address is not the same with numPtr1, numPtr2?
For example, let num's address 0x7fffaca780b4. Then when I run this code, the output is
10
10
10
0x7fffaca780b4
0x7fffaca780b4
0x7fffaca780b8
Sorry for my bad english

numPtr1 and numPtr2 are two different variables. So where those variables are located will be different, regardless of where they point to.

numptr2 is pointing to numptr1 varible,numptr1 is pointing to num.
So in numptr2 address of numptr1 will be stored & in numptr1 address of num will be stored
both(numptr1,num) addresses are different.
this is the reason that the you get different address.

Why numPtr2's address is not the same with numPtr1, numPtr2?
Each variable has its own address in memory so their content may be same but their address can't be same otherwise it would be impossible to differentiate them.

About [num]
value of [num] = 10
memory location of [num] = 0115FC14
About [numPtr1]
value of [numPtr1] when it dereferenced = 10
address of [numPtr1] is holding = 0115FC14
memory location of [numPtr1] = 0115FC08
About [numPtr2]
value of [numPtr2] when it dereferenced once = 0115FC14(*numPtr2)
value of [numPtr2] when it dereferenced twice = 10(**numPtr2)
address of [numPtr2] holding = 0115FC08(numPtr2) is equals to memory location of [numPtr1] = 0115FC08(&numPtr1)
memory location of [numPtr2] = 0115FBFC
Name
num
numPtr1
numPtr2
Value
10
0115FC14
0115FC08
Memory location
0115FC14
0115FC08
0115FBFC
Here is the code
#include <stdio.h>
int main(void)
{
int a = 5;
int* a_ptr1 = &a;
int** a_ptr2 = &a_ptr1;
puts("***About [a]***");
printf("value of [a] = %d\nmemory location of [a] = %p\n\n", a, &a);
puts("***About [a_ptr1]***");
printf("value of [a_ptr1] when it dereferenced = %d\n", *a_ptr1);
printf("address of [a_ptr1] is holding = %p\n", a_ptr1);
printf("memory location of [a_ptr1] = %p\n\n", &a_ptr1);
puts("***About [a_ptr2]***");
printf("value of [a_ptr2] when it dereferenced once = %p(*a_ptr2)\n", *a_ptr2);
printf("value of [a_ptr2] when it dereferenced twice = %d(**a_ptr2)\n", **a_ptr2);
printf("address of [a_ptr2] holding = %p(a_ptr2) ", a_ptr2);
printf("is equals to memory location of [a_ptr1] = %p(&a_ptr1)\n", &a_ptr1);
printf("memory location of [a_ptr2] = %p\n\n", &a_ptr2);
printf("Name\t\t[a]<---------[a_ptr1]<-------[a_ptr2]\n");
printf("Value\t\t%d %p %p\n", a, a_ptr1, a_ptr2);
printf("Address\t\t%p %p %p\n", &a, &a_ptr1, &a_ptr2);
}

Related

add local variable and pointer variable

I'm a beginner in C language.
I'm practicing several codes on my own, while doing I came across this algo.
Below is the code.
#include <stdio.h>
int main() {
int a = 1;
printf("value of a = %d\n", a);
printf("address of a = %u\n", &a);
int *p;
printf("value of p = %d\n", p);
p = 2;
printf("value of p = %d\n", p);
a = a+p;
printf("value of addition =%d\n", a);
return 0;
}
**OUTPUT**
value of a = 1
address of a = 947268620
value of p = 947268880
value of p = 2
value of addition =6
Why I'm getting 6 instead of 3,
is there anything I'm missing on result
because you're not setting the value of the address p is pointing to to 2, you're assigning/pointing the variable int *p to the value 2 in memory, which is not memory you should be accessing. Instead, you need to point p to memory you have access to (a variable or dynamically allocated memory) and dereference the pointer using *p = 2 which accesses the value that p is pointing to. Your code should instead look like this
int a = 1;
printf("value of a = %d\n", a);
printf("address of a = %u\n", &a);
int _p = 0;
int *p = &_p;
printf("value of p = %d\n", *p);
*p = 2;
printf("value of p = %d\n", *p);
a = a+*p;
printf("value of addition =%d\n", a);
One has to "walk right past" the compiler warnings about that code.
Below is the same code with coercive casting to silence some of the warnings. An additional calculation and print statement should make obvious how the compiler deals differently with the values of pointers (memory addresses) and the values of integers.
#include <stdio.h>
int main() {
int a = 1;
printf("value of a = %d\n", a);
printf("address of a = %u\n", &a); // incorrect format spec for printing address
int *p;
printf("value of p = %d\n", p); // uninitialised and undefined behaviour
p = (int*)2; // coercive casting integer to pointer-to-integer
printf("value of p = %d\n", p); // incorrect format spec for a memory address
a = (int)(a + p); // coercive casting address to integer
printf("value of addition =%d\n", a);
// ADDED these statements
a = 1; // restore value of a
a = a + (int)p; // coercive casting address to integer
printf("value of addition =%d\n", a);
return 0;
}
value of a = 1
address of a = 1703728
value of p = 1
value of p = 2
value of addition =6
value of addition =3 <== was this the expected result?
C will try to do its best with explicit program statements.
"Garbage in, garbage out."
Write correct code, not 'cute' code.
Thanks for your response Awayy. Helpful.
I got the output I needed. But the thing I need to know is, in what logic
the line a = a+p; gave 6 as result in my code.
As u said I'm assigning 2 as address to p(which is p = 0x2).
So, when the addition happens a = 1 and p = 0x2 which has a random value that we don't know that is because I'm not assigning a particular address to the pointer variable p. It results 6 I'm I right.

Assigning a pointer to the exact next memory address of an int

I am trying to assign a pointer to the exact next memory address of an int number and give it the doubled value of the number. However i can only write after 3(*4 which is the size of int) or more memory addresses,else it doesnt print anything.
That works
#include <stdio.h>
int main(){
int x, *ptr, memory_jump = 3;
scanf("%d",&x);
ptr = &x+memory_jump;
*ptr = x * 2;
printf("Given int = %d, Address = %d \nNext address content = %d,Next address pos = %d",x,&x,*ptr,ptr);
return 0;
}
If i lower the memory jump to 2 or 1 it doesnt print anything at all so an error occurs.
int main(){
int x, *ptr, memory_jump = 2;
scanf("%d",&x);
ptr = &x+memory_jump;
*ptr = x * 2;
printf("%d", (ptr-2));
printf("Given int = %d, Address = %d \nNext address content = %d,Next address pos = %d",x,&x,*ptr,ptr);
return 0;
}
Could anyone explain what happens with these 2 next memory addresses?
Your "experiments" are flawed.
In both cases, you are accessing memory locations and assigning values as if there's a valid int object at those locations. And both are undefined behaviour.

How to double deref in C?

Trying to double dereference and print them (TOP TWO ARE EXAMPLES):
printf ("a's value = %d \n", a) ;
printf ("a's address = %p \n", &a) ;
printf ("a_ptr_ptr deref'ed defer'ed =d% \n",
What would go after the \n", for a_ptr_ptr deref'ed defer'ed
If you want the address of the address of a, you're going to have to store a's address in a pointer variable, and take the address of that. But having done so, yes, you can double-dereference that pointer with **, and get a's value back. Something like this:
int a = 5;
int *ip = &a;
int **ipp = &ip;
printf("ipp = %p\n", ipp);
printf("*ipp = %p, ip = %p, &a = %p\n", *ipp, ip, &a);
printf("**ipp = %d, *ip = %d\n", **ipp, *ip);
Theoretically you can continue this as long as you like:
int ***ippp = &ipp;
int ****ipppp = &ippp;
int *****ippppp = &ipppp;
printf("*****ippppp = %d\n", *****ippppp);
But by now this is mostly a game; there's no practical use in a real C program for a 5-level pointer, and at some point (after 8 or 10 levels, I think) the compiler's allowed to say "All right, enough, game over!".

Why can't I swap memory address of two variables using a function? C

static void swapAddr(int *numOne, int *numTwo)
{
int *tmp;
tmp = numOne;
numOne = numTwo;
numTwo = tmp;
}
int main(void)
{
int a = 15;
int b = 10;
printf("a is: %d\n", a);
printf("Address of a: %p\n", &a);
printf("b is: %d\n", b);
printf("Address of b: %p\n", &b);
swapAddr(&a, &b);
printf("\n");
printf("a is: %d\n", a);
printf("Address of a: %p\n", &a);
printf("b is: %d\n", b);
printf("Address of b: %p\n", &b);
return 0;
}
When I compile and run this piece of code, the output is
a is: 15
Address of a: 0x7fff57f39b98
b is: 10
Address of b: 0x7fff57f39b94
a is: 15
Address of a: 0x7fff57f39b98
b is: 10
Address of b: 0x7fff57f39b94
Clearly the result is not what I intended, since the address does not seem to have been swapped at all.
You generally can't change the address of a variable.
Your 'swapAddr' function changes its parameter values, but these are local to the function - you're not changing anything outside the function. Perhaps the best way of understanding it is that a function parameter always receives a copy of the value that was passed to the function. In this case, you get a copy of the address of a and a copy of the address of b. You can and do change the values of the variables holding those copies (numOne and numTwo), and seeing as they are pointers you could (but don't) change the values that they point at (the values of variables a and b) - but you can't change the addresses of the original variables.
To break it down:
static void swapAddr(int *numOne, int *numTwo)
{
int *tmp;
tmp = numOne;
At this point, tmp and numOne both point to the value of the a variable...
numOne = numTwo;
Now, numOne points instead to the value of the b variable...
numTwo = tmp;
}
And finally, numTwo now points to the value of the a variable. The function returns and numOne and numTwo no longer exist after that point. The addresses of the variables a and b did not change at any stage.
You could however write a function which exchanges the addresses in two pointer variables:
static void swapAddr(int **ptrOne, int **ptrTwo)
{
int *tmp;
tmp = *ptrOne;
*ptrOne = *ptrTwo;
*ptrTwo = tmp;
}
This would allow you to pass the address of two pointer variables, and on return the pointers would be swapped - each one pointing at what the other did previously. But again, this would not change the address of any variable that those pointers happened to point to.
The pointers are passed to the function by value, so changing what they point to isn't going to change the value of the passed parameters in the calling function.
When the function is called, a copy of each pointer is made and saved to the stack. Then the function reads each pointer value off the stack and manipulates them. It never changes the value of the original pointer that was copied onto the stack.
Remember that in C values are passed by value to functions, meaning that the values are copied. When you modify an argument in a function you only modify the local copy inside the function, not the original value that was passed to the function. This goes for pointers as well.
To solve your problem you must pass the arguments by reference, but unfortunately C doesn't have that, it only have pass by value. However, pass by reference can be emulated by passing pointers to the data, just like you do in the function. You must however dereference the pointer to get the values from where the pointers point to, and use those values to do the actual swapping:
int temp = *numOne; // Note: temp is a value not a pointer
*numOne = *numTwo;
*numTwo = temp;
static void swapAddr(int *numOne, int *numTwo)
In this function you are passing 2 pointers by value. This allows you to modify the int pointed to by the pointers but not the pointers themselves.
Use this function definition instead that passes pointers to pointers and allows modifying the pointers themselves
static void swapAddr(int **numOne, int **numTwo) {
int *tmp = *numOne;
numOne = *numTwo;
numTwo = tmp;
}
You could use it like this for example:
int *a = malloc(sizeof(int));
int *b = malloc(sizeof(int));
*a = 15;
*b = 10;
swapAddr(&a, &b);
You canlt change the addresses. The adderss of a is the address of a and that will remain the same until the end of days.
You can do:
static void swapAddr(int **numOne, int **numTwo)
{
int *tmp;
tmp = *numOne;
*numOne = *numTwo;
*numTwo = tmp;
}
int main(void)
{
int a = 15;
int b = 10;
int *pa= &a;
int *pb= &b;
swapAddr(&pa, &pb);
}
What you want to achieve is something like
int *c = &a;
&a = &b;
&b = &a;
This is not possible (you can check: it will not compile). A variable that is created is placed at one place in memory and stays there. So when you create a variable a it will stay variable a and it will not be able to change its identity to that of another variable b.
What you can do is use two pointers int *p1, *p2 to int. These pointers can change their value and point to other objects during lifetime:
p1 = a;
p2 = b;
p1 = b;
p2 = a;
a and b will stay the same, but p1 and p2 can point to different objects over time.
So a thing that would be possible:
static void swapaddr(int **pp1, int **pp2)
{
int *pp;
pp = *pp1;
*pp1 = *pp2;
*pp2 = pp;
}
int main(void)
{
int a = 15, b = 10;
int *pA = &a, *pB = &b;
swapAddr(&pA, &pB);
}
In this example a and b would keep their identity and address, but pA and pB would change their value and pA would point to b and pB would point to pA.
You cannot change the addresses of the variables.however you can change values of pointers,which store addresses as their value,here is an example :
#include <stdio.h>
void swapAddr(int **numOne, int **numTwo)
{
int *tmp;
tmp = *numOne;
*numOne = *numTwo;
*numTwo = tmp;
}
int main(void)
{
int a = 15;
int b = 10;
int *p_a = &a;
int *p_b = &b;
printf("Address of a: %p\n", p_a);
printf("Address of b: %p\n", p_b);
swapAddr(&p_a,&p_b);
printf("\n");
printf("p_a : %p\n",p_a);
printf("p_b : %p\n",p_b);
return 0;
}

Pointer to memory

I'm learning C and have some problems whit pointers.
I'm triying to print the memory slot for every declared variable, but when I declare the pointer for a Char[], it just does not work.
Here's my code:
int main () {
char a[3]; // this variable is my problem
int b;
float c;
char d;
int e=4;
char *pachar; //A char type variable for the pointer.
int *paint;
float *pafloat;
char *pacharr;
int *paintt;
pachar = &a; // when I try to assign the memory to the pointer, it shows a Warning message.
paint = &b;
pafloat = &c;
pacharr = &d;
paintt = &e;
printf("%p \n",pachar);
printf("%p \n",paint);
printf("%p \n",pafloat);
printf("%p \n",pacharr);
printf("%p \n",paintt);
return(0);
}
This is the warning message. Am I doing something wrong?
"warning: assignment from incompatible pointer type"
You declared a as an array of char:
char a[3];
Name a represents an array, which can be interpreted as a pointer to the initial element of the array. Therefore you do not need & when you assign a to a pointer:
pachar = a;
When you take an address of a in &a expression, you get a pointer to an array of three characters. Trying to assign a pointer-to-an-array to a pointer-to-char triggers compiler warning.
Instead of taking the address of a, you could take the address of the first element. The address of an array is the same as the address of its first element.
pachar = &a[0];
char a[3], *pachar;
pachar = a;
printf("Address = %u\n", a); // Base address of the Array. "&a" not required.
printf("Address = %u\n", &a[0]); // Address of the first element.
printf("Address = %u\n", pachar);// Address of the first element.
printf("Address = %u\n", pachar + 0); // Pointer Arithmetic ...
printf("Address = %u\n", pachar + 1); // a[1] is equivalent to pachar + 1
printf("Address = %u\n", &a[1]); // Similar to the above.

Resources