How to double deref in C? - 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!".

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.

Calculate the sum of two numbers and "return" it by using a void function in C with a pointer as parameter

So basically I'd like to sum two numbers and return their value while using a void function in C. I know this is easy peasy by using a normal function returning an int or a numeric type but I wanna work on my pointer knowledge.
I tried creating a pointer inside main() and then passing it as an argument to the void function. Then I calculated my sum in a new int variable and assigned the pointer to point to that specific variable. The problem is I can't "retrieve" it or "find" that area of memory in the main function.
Here's what I've tried:
void testFunction(int a,int b, int *x)
{
int c=a+b;
x=&c;
}
int main()
{
int n1=7;
int n2=90;
int *pointerParam;
testFunction(n1, n2, pointerParam);
printf("Value of pointer is %d\n", *pointerParam);
}
It just exits with an error code, it does nothing. If I try to printf *x inside the function, it does work so I know that part at least works.
Any help would be greatly appreciated!
There are multiple problems with the code as it is shown.
The main problem is probably that you misunderstand how emulation of pass-by-reference works in C.
For it to work you need to pass a pointer to the variable that should be set. This is done using the pointer-to operator &. You also need to dereference the pointer, to set the variable the pointer is pointing to.
Putting it together your program should look something like this (simplified):
void testFunction(int a,int b, int *x)
{
// Assign to where `x` is pointing
*x = a + b;
}
int main(void)
{
int n1 = 7;
int n2 = 90;
int result; // Where the result should be written
// Pass a pointer to the `result` variable, so the function can write to it
testFunction(n1, n2, &result);
}
I will show you a program that is no more than a set of printf() and your function. Maybe the program's output helps in showing these pointers, arrays and integers things.
The example
Consider these variables
int n[4] = {10, 20, -30, -40};
int v1 = 0;
int v2 = 0;
int* a_pointer = NULL;
int* is a pointer to an int, that's the meaning of the asterisk in the declaration. In this context the asterisk is called the dereference operator or the indirection operator. But the asterisk also is the multiplication operator in C. ;)
Now a_pointer points to nothing, the meaning of the NULL.
But a_pointer is there to hold an address of something, of an int. The way of getting such address of something is the address of operator, the &, that also has an alternate life as the bitwise and operator in C. Things of life.
In printf() the %p specifier shows an address. This
printf("\naddress of v1 is %p\n", &v1);
printf("address of v2 is %p\n", &v2);
printf("address of array n[0] is %p\n", n);
printf("address of array n[0] is %p\n", 1 + n);
printf("address of array n[0] is %p\n", 2 + n);
printf("address of array n[0] is %p\n\n", 3 + n);
shows (in a 32-bits compilation)
address of v1 is 008FFDA0
address of v2 is 008FFD9C
address of array n[0] is 008FFDA4
address of array n[1] is 008FFDA8
address of array n[2] is 008FFDAC
address of array n[3] is 008FFDB0
And you will see the reason the program prints these lines in the code below...
This line
a_pointer = &v1;
takes the address of v1 and assign it to the pointer a_pointer.
Now a_pointer is pointing to something, to v1, and you can use it in your function. These lines are equivalent
testFunction(n[0], n[3], &v1);
and
testFunction(n[0], n[3], a_pointer);
In these lines
testFunction(n[0], n[3], &v1);
printf("n[0] = %d, n[3] = %d, sum in v1 is %d\n", n[0], n[3], v1);
printf(
"p points to v1. value is %4d, address is %p\n\n", *a_pointer,
a_pointer);
you see the use of the pointer to access the value it points to, using the dereference operator in the printf().
Follow the program along to see a few uses of this.
In particular, see these lines
a_pointer = n + 3;
printf(
"\np points now to n[3]. value is %4d, address is %p\n",
*a_pointer, a_pointer);
to see the thing C is made for: address memory easily. n is int[4], an array of int. a_pointer is a pointer to int. And the language knows that when you write a_pointer = n + 3 that it needs to add to the address of n, an int[], the size of 3 int variables, and assign it to the pointer, so *a_pointer is n[3]and it is used to call testFunction() in
testFunction(n[1], n[2], n + 3);
program output
n[] is [10,20,-30,-40], v1 is 0 v2 is 0
address of v1 is 00CFFA9C
address of v2 is 00CFFA98
address of array n[0] is 00CFFAA0
address of array n[1] is 00CFFAA4
address of array n[2] is 00CFFAA8
address of array n[3] is 00CFFAAC
n[0] = 10, n[3] = -40, sum in v1 is -30
p points to v1. value is -30, address is 00CFFA9C
p now points to v2. value is 0, address is 00CFFA98
n[0] = 10, n[1] = 20, sum in v2 is 30
n[] is [10,20,-30,-40], v1 is -30 v2 is 30
p now points to v1. value is -30, address is 00CFFA9C
n[] is [10,20,-30,-40], v1 is -30 v2 is 30
now makes n[3] = n[1] + n[2] using testFunction()
n[] is [10,20,-30,-10], v1 is -30 v2 is 30
p points now to n[3]. value is -10, address is 00CFFAAC
the code
#include <stdio.h>
show(int[4], int, int);
void testFunction(int, int, int*);
int main(void)
{
int n[4] = {10, 20, -30, -40};
int v1 = 0;
int v2 = 0;
int* a_pointer = NULL;
show(n, v1, v2);
printf("\naddress of v1 is %p\n", &v1);
printf("address of v2 is %p\n", &v2);
printf("address of array n[0] is %p\n", n);
printf("address of array n[1] is %p\n", 1 + n);
printf("address of array n[2] is %p\n", 2 + n);
printf("address of array n[3] is %p\n\n", 3 + n);
a_pointer = &v1;
testFunction(n[0], n[3], &v1);
printf("n[0] = %d, n[3] = %d, sum in v1 is %d\n", n[0], n[3], v1);
printf(
"p points to v1. value is %4d, address is %p\n\n", *a_pointer,
a_pointer);
a_pointer = &v2;
printf(
"p now points to v2. value is %4d, address is %p\n", *a_pointer,
a_pointer);
testFunction(n[0], n[1], &v2);
printf("n[0] = %d, n[1] = %d, sum in v2 is %d\n", n[0], n[1], v2);
show(n, v1, v2);
a_pointer = &v1;
printf(
"\np now points to v1. value is %4d, address is %p\n", *a_pointer,
a_pointer);
show(n, v1, v2);
printf("\nnow makes n[3] = n[1] + n[2] using testFunction()\n");
testFunction(n[1], n[2], n + 3);
show(n, v1, v2);
a_pointer = n + 3;
printf(
"\np points now to n[3]. value is %4d, address is %p\n",
*a_pointer, a_pointer);
return 0;
};
show(int n[4], int v1, int v2)
{
printf(
"n[] is [%d,%d,%d,%d], v1 is %d v2 is %d\n", n[0], n[1], n[2],
n[3], v1, v2);
};
void testFunction(int a, int b, int* sum)
{
*sum = a + b;
return;
}
I will not go into religious discussions here, but you may find easier to understand the meaning of the declarions if you write
int* some_int = NULL;
instead of
int *some_int = NULL;
you declare a name, and the name is some_int. The compiler will tell you that some_int is int*, its type. The fact that *some_int is an int is a consequence of the application of an operator to a variable.
You lacked just to understand how pointers are managed, but you were almost correct:
/* this is almost correct, you could have just said: *x = a + b; */
void testFunction(int a,int b, int *x)
{
int c=a+b;
*x=c; /* the pointed to value is what we are assigning */
}
int main()
{
int n1=7;
int n2=90;
int result; /* vvvvvvv this is the important point */
testFunction(n1, n2, &result); /* you pass the address of result as the required pointer */
printf("Value of pointer is %d\n", result);
}

pointer to pointer address

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);
}

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.

The difference between pointer address

I am a Java programmer and recently play with C for fun. Now I am learning address and pointers which are a little bit confusing for me. Here is my question. See the below two blocks of the code.
void withinArray(int * a, int size, int * ptr) {
int x;
printf("ptr is %d\n", ptr);
printf("a is %d\n", a);
printf("difference in pointers is: %d\n", ptr - a);
x = ptr - intArray;
printf("x is %d\n", x);
}
void doubleSize() {
double doubArray[10];
double * doubPtr1;
double * doubPtr2;
doubPtr1 = doubArray;
doubPtr2= doubArray+1;
int p2 = doubPtr2;
int p1 = doubPtr1;
printf("p2-p1 is %d\n", p2-p1);
printf("doubPtr2-doubPtr1 is %d\n", doubPtr2-doubPtr1);
}
int main(void)
{
int a[10];
int *intarray = a;
int *p = intarray + 9;
printf(withinArray(a, 10, p));
return 0;
}
I am wondering for function withinArray(), why we could directly get the x value, which is 9? But for the other method, we have to convert doubPtr to int first and then we can get the difference between pointers in int?
From my understanding, in doubleSize(), doubPtr2-doubPtr1 = 1 means the difference in pointer address in memory is 1. But why the withinArray() doesn't need to do so?
A difference of 1 between two pointers means that the pointers point to adjacent units of memory of the size of the objects pointed at.
Thus, given:
int i[2];
int *ip0 = &i[0];
int *ip1 = &i[1];
double d[2];
double *dp0 = &d[0];
double *dp1 = &d[1];
we could safely write:
assert((ip1 - ip0) == (dp1 - dp0));
assert(ip1 - ip0 == 1);
assert(dp1 - dp0 == 1);
However, you could also safely write:
assert((char *)ip1 - (char *)ip0 == sizeof(int));
assert((char *)dp1 - (char *)dp0 == sizeof(double));
and usually you would find that it is safe to write:
assert(sizeof(double) != sizeof(int));
though that is not guaranteed by the standard.
Also, as Filipe Gonçalves correctly points out in his comment, the difference between two pointers is formally only defined if the pointers are of the same type and point to two elements of the same array, or point to one element beyond the end of the array. Note that standard C demands that given:
int a[100];
it is safe to generate the address int *ip = &array[100];, even though it is not safe to either read from or write to the location pointed at by ip. The value stored in ip can be used in comparisons.
You also formally cannot subtract two void * values because there is no size for the type void (which is why my example used casts to char *, not void *). Beware: GCC will not object to the subtraction of two void * values unless you include -pedantic in the options.
Do you know why the value of doubPtr2 - doubPtr1 (in my second method) is different from x = ptr - a (in my first method)?
Assuming that intArray is meant to be a, then this code:
#include <stdio.h>
static void withinArray(int *a, int *ptr)
{
int x;
printf("ptr is %p\n", (void *)ptr);
printf("a is %p\n", (void *)a);
printf("difference in pointers is: %td\n", ptr - a);
x = ptr - a;
printf("x is %d\n", x);
}
static void doubleSize(void)
{
double doubArray[10];
double *doubPtr1 = doubArray;
double *doubPtr2 = doubArray+1;
int p2 = doubPtr2;
int p1 = doubPtr1;
printf("p1 = 0x%.8X\n", p1);
printf("p2 = 0x%.8X\n", p2);
printf("p2-p1 is %d\n", p2-p1);
printf("doubPtr1 = %p\n", (void *)doubPtr1);
printf("doubPtr1 = %p\n", (void *)doubPtr2);
printf("doubPtr2-doubPtr1 is %td\n", doubPtr2-doubPtr1);
}
int main(void)
{
int a[10];
int *intarray = a;
int *p = intarray + 9;
withinArray(a, p);
doubleSize();
return 0;
}
compiles with warnings that I would ordinarily fix (change the type of p1 and p2 to uintptr_t, include <inttypes.h>, and format using "p1 = 0x%.8" PRIXPTR "\n" as the format string), and it generates the output:
ptr is 0x7fff5c5684a4
a is 0x7fff5c568480
difference in pointers is: 9
x is 9
p1 = 0x5C5684B0
p2 = 0x5C5684B8
p2-p1 is 8
doubPtr1 = 0x7fff5c5684b0
doubPtr1 = 0x7fff5c5684b8
doubPtr2-doubPtr1 is 1
Fixed code generates:
ptr is 0x7fff5594f4a4
a is 0x7fff5594f480
difference in pointers is: 9
x is 9
p1 = 0x7FFF5594F4B0
p2 = 0x7FFF5594F4B8
p2-p1 is 8
doubPtr1 = 0x7fff5594f4b0
doubPtr1 = 0x7fff5594f4b8
doubPtr2-doubPtr1 is 1
(The difference is in the number of hex digits printed for p1 and p2.)
I assume that your puzzlement is about why the int code prints 9 rather than, say, 36, whereas the double code prints 8 instead of 1.
The answer is that when you subtract two pointers, the result is given in units of the size of the objects pointed at (which I seem to remember saying in my opening sentence).
When you execute doubPtr2-doubPtr1, the distance returned is in units of the number of double values between the two addresses.
However, the conversion to integer loses the type information, so you effectively have the char * (or void *) addresses of the two pointers in the integer, and the byte addresses are indeed 8 apart.
If we make two symmetrical routines, the information is clearer:
#include <stdio.h>
#include <inttypes.h>
static void intSize(void)
{
int intArray[10];
int *intPtr1 = intArray;
int *intPtr2 = intArray+1;
uintptr_t p2 = (uintptr_t)intPtr2;
uintptr_t p1 = (uintptr_t)intPtr1;
printf("p1 = 0x%.8" PRIXPTR "\n", p1);
printf("p2 = 0x%.8" PRIXPTR "\n", p2);
printf("p2-p1 is %" PRIdPTR "\n", p2-p1);
printf("intPtr1 = %p\n", (void *)intPtr1);
printf("intPtr1 = %p\n", (void *)intPtr2);
printf("intPtr2-intPtr1 is %td\n", intPtr2-intPtr1);
}
static void doubleSize(void)
{
double doubArray[10];
double *doubPtr1 = doubArray;
double *doubPtr2 = doubArray+1;
uintptr_t p2 = (uintptr_t)doubPtr2;
uintptr_t p1 = (uintptr_t)doubPtr1;
printf("p1 = 0x%.8" PRIXPTR "\n", p1);
printf("p2 = 0x%.8" PRIXPTR "\n", p2);
printf("p2-p1 is %" PRIdPTR "\n", p2-p1);
printf("doubPtr1 = %p\n", (void *)doubPtr1);
printf("doubPtr1 = %p\n", (void *)doubPtr2);
printf("doubPtr2-doubPtr1 is %td\n", doubPtr2-doubPtr1);
}
int main(void)
{
doubleSize();
intSize();
return 0;
}
Output:
p1 = 0x7FFF5C93D4B0
p2 = 0x7FFF5C93D4B8
p2-p1 is 8
doubPtr1 = 0x7fff5c93d4b0
doubPtr1 = 0x7fff5c93d4b8
doubPtr2-doubPtr1 is 1
p1 = 0x7FFF5C93D4B0
p2 = 0x7FFF5C93D4B4
p2-p1 is 4
intPtr1 = 0x7fff5c93d4b0
intPtr1 = 0x7fff5c93d4b4
intPtr2-intPtr1 is 1
Remember Polya's advice in How to Solve It:
Try to treat symmetrically what is symmetrical and do not destroy wantonly any natural symmetry.

Resources