int* a[2];
int x = 5;
int y = 7;
int *b = x;
int *c = y;
a[0] = b;
a[1] = c;
printf("%d", *a[1]);
Why isn't this working?
This is an Array of pointers and I'm trying to get the value of y - a[1] is the address of y so I added a * to get the value of it..
Thanks everyone!
int *b = x;
int *c = y;
The problem here is that your are assigning the values of the objects x and y instead of their address.
You can get the address of an object using the address-of operator &.
int *b = x;
int *c = y;
Why isn't this working?
Because you assign the value held by the integer to the pointer, not the reference of the variable x or y
You need to:
int *b = &x;
int *c = &y;
A slight change to your code to pass the address of the integer x and y to pointers b and c will make your code work.
int *b = &x;
int *c = &y;
This question already has answers here:
Why use double indirection? or Why use pointers to pointers?
(18 answers)
Closed 1 year ago.
I know that int *p is a pointer but what does int **p mean exactly? What type of value is that? When I say now p= something, how is that working? I am seeing this in the creation of two-dimensional arrays with pointers.
In short, int **p; is a pointer to a pointer to an int. So, for example:
int i, j; // Integers;
int *p = &i; // Pointer to i
*p = 1; // i is now 1
int *q = &j; // Pointer to j
int **s = &p; // Pointer to p
**s = 2; // i is now 2
s = &q; // s now points to q (pointing to j)
**s = 16; // j is now 16
s = &p; // s now points to p (pointing to i)
**s = 3; // i is now 3
p = &j; // p now points to j
**s = 17; // j is now 17
One use case for a pointer to pointer could be a function which needs to output a pointer to an int and return a success/failure status:
#include <stdbool.h>
bool getIntHandle(unsigned handleId, int **handle)
{
static int handles[] = {12, 23, 34, 45};
bool success = (handleId < sizeof(handles)/sizeof(handles[0])); // Check that handleId is in range
if(success)
{
*handle = &handles[handleId];
}
return success;
}
int main(void)
{
int *handle;
bool success = getIntHandle(2, &handle); // Get a pointer to the integer at index 2
printf("*handle = %d\n", *handle);
return 0;
}
p is a pointer to a pointer to an int. It's the type int **p and the variable p stores an address.
Here is an example of its use. p is an array of two integer pointers. The first of the pointers p[0] points to an array of 3 integers, and the 2nd to an array of 4 integers (combined this is known as ragged array):
#include <stdio.h>
#include <stdlib.h>
int main() {
int **p = malloc(2 * sizeof(int *));
printf("p = %p\n", p);
p[0] = malloc(3 * sizeof(int));
printf("p[0] = %p\n", p[0]);
p[0][0] = 0;
p[0][1] = 1;
p[0][2] = 2;
p[1] = malloc(4 * sizeof(int));
printf("p[1] = %p\n", p[1]);
p[1][0] = 3;
p[1][1] = 4;
p[1][2] = 5;
p[1][3] = 6;
free(p[0]);
free(p[1]);
free(p);
return 0;
}
The most common use, however, is with a regular 2d array that is passed to a function which degrades to a pointer to a pointer.
Trying to understand how a pointer works in a function that returns an array.
When the temp array is returned to the function, why is it that p[0] is 1 and p[1] is 3? Since x and y variable are swapped within the function and temp[0] and temp[1] are not swapped.
int *swap(int *x, int *y){
static int temp[2];
temp[0] = *x;
temp[1] = *y;
*x = temp[1];
*y = temp[0];
return temp;
}
int main() {
int x = 3;
int y = 1;
int *p = swap(&x, &y);
GPIO_PORTF_AHB_DATA_BITS_R[LED_RED] = LED_RED;//turn on red led
delay(p[0]);
GPIO_PORTF_AHB_DATA_BITS_R[LED_RED] = 0;//turn off red led
delay(p[1]);
}
why is it that p[0] is 1 and p[1] is 3
It isn't.
Replacing your microcontroller-specific code with:
printf("p[0] = %d, p[1] = %d\n", p[0], p[1]);
and running your code on a computer gives me the output:
p[0] = 3, p[1] = 1
as expected.
I am studying C and I've been given a task.
"Modify the program by adding a new variable that stores the address of x. Then use your variable to update (indirectly) the value of i and then print out the new value to demonstrate that your modification has worked."
This is the code I have to modify:
#include <stdio.h>
int main()
{
int i, j;
int * p, * q;
int ** x;
i = 100;
j = 200;
p = &i;
q = &j;
x = &p;
*p = *p + *q;
*q = **x / 2;
**x = *p + j;
printf(" i = %d\n", i);
printf("&i = %p\n", &i);
printf(" j = %d\n", j);
printf("&j = %p\n", &j);
printf(" p = %p\n", p);
printf("&p = %p\n", &p);
printf("*p = %d\n", *p);
printf(" q = %p\n", q);
printf("&q = %p\n", &q);
printf("*q = %d\n", *q);
printf(" x = %p\n", x);
printf("&x = %p\n", &x);
printf("*x = %p\n", *x);
printf("**x= %d\n", **x);
return 0;
}
This is what I have attempted. I declared the variable that stores the address. I then assigned it the address of x of x (newVariable) and then tried to create an increment update of address i. Compiling the program gave me the following errors:
ptr3.c:14:18: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
*newVariable = &x; /* Assign new variable address of x */
^
ptr3.c:19:19: error: lvalue required as unary ‘&’ operand
newVariable = &i++; /* Autoincrement address by 1 */
^
ptr3.c:21:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("&newVariable = %d\n", newVariable); /* New printf statement */
^
The code:
#include <stdio.h>
int main()
{
int i, j;
int * p, * q;
int ** x;
int * newVariable; /* New variable of type address*/
i = 100;
j = 200;
p = &i;
q = &j;
x = &p;
newVariable = &x; /* Assign new variable address of x */
*p = *p + *q;
*q = **x / 2;
**x = *p + j;
newVariable = &i++; /* Autoincrement address by 1 */
printf("&newVariable = %d\n", newVariable); /* New printf statement */
printf(" i = %d\n", i);
printf("&i = %p\n", &i);
printf(" j = %d\n", j);
printf("&j = %p\n", &j);
printf(" p = %p\n", p);
printf("&p = %p\n", &p);
printf("*p = %d\n", *p);
printf(" q = %p\n", q);
printf("&q = %p\n", &q);
printf("*q = %d\n", *q);
printf(" x = %p\n", x);
printf("&x = %p\n", &x);
printf("*x = %p\n", *x);
printf("**x= %d\n", **x);
return 0;
}
Version 3:
#include <stdio.h>
int main()
{
int i, j;
int * p, * q;
int ** x;
int * newVariable; /* New variable of type address*/
i = 100;
j = 200;
p = &i;
q = &j;
x = &p;
newVariable = &x; /* Assign new variable address of x */
*p = *p + *q;
*q = **x / 2;
**x = *p + j;
newVariable = &i++; /* Autoincrement address by 1 */
printf("&newVariable = %d\n", newVariable); /* New printf statement */
printf(" i = %d\n", i);
printf("&i = %p\n", &i);
printf(" j = %d\n", j);
printf("&j = %p\n", &j);
printf(" p = %p\n", p);
printf("&p = %p\n", &p);
printf("*p = %d\n", *p);
printf(" q = %p\n", q);
printf("&q = %p\n", &q);
printf("*q = %d\n", *q);
printf(" x = %p\n", x);
printf("&x = %p\n", &x);
printf("*x = %p\n", *x);
printf("**x= %d\n", **x);
return 0;
}
Could someone tell me what I have done wrong? I fail to see what is wrong with the program.
If the new variable will hold the address of x, it needs to be a
pointer to the type of x which is int **. So:
int ***newVariable;
You're assigning the address of x correctly, with:
newVariable = &x;
To modify i, let's think a little. The original x itself points
to something else (in this case p) that points to i. So
the newVariable adds an extra level, meaning you just have to
dereference it three levels to get access to i. If, for example, you
want to modify i by incrementing it you can then do it with:
***newVariable += 3; // increment i (indirectly) by 3
To print the value of i you can use itself directly, or p which
points to it, or x which points to p, or newVariable which
points to x. So these statements will yield the same result:
printf("i = %d\n", i);
printf("i = %d\n", *p);
printf("i = %d\n", **x);
printf("i = %d\n", ***newVariable);
You shouldn't provide two main() functions in one file.
First problem is at newVariable definition:
int ** x; /* pointer to pointer */
/* Since you want to store adress of x, you should declare pointer to pointer to pointer */
int *** newVariable; /* variable to store adress of x */
Second problem is on how do you indirectly increment i value:
/* Since newVariable -> x -> p -> i (where "->" means "is a pointer to") */
(***newVariable)++; /* increment value of i by 1 */
You need to use ***newVar and point to address of x.Then you can change pointed value of newVar and show that x is also changed.
#include <stdio.h>
int main()
{
int i, j;
int * p, * q;
int ** x;
int *** newVar;
i = 100;
j = 200;
p = &i;
q = &j;
x = &p;
newVar = &x;
// prove that newVar is pointing to same as x
printf("**x= %d\n", **x); //100
***newVar = 12345;
printf("***newVar= %d\n", ***newVar); //12345
printf("**x= %d\n", **x); //12345
printf("*p= %d\n", *p); //12345
printf("i= %d\n", i); //12345
//prove that they are all pointing to same address
printf("p= %p\n", p);
printf("*x= %p\n", *x);
printf("**newVar= %p\n", **newVar);
return 0;
}
I have a C program
#include<stdio.h>
void f(const int* p)
{
int j;
p = &j;
j = 10;
printf("Inside function *p = %d\n",*p);
*p = 5;
printf("Inside function *p = %d\n",*p);
j = 7;
printf("Inside function *p = %d\n",*p);
}
int main()
{
int i = 20, *q = &i;
f(q);
}
Compilation of the program gives the error
Assignment of the read only location *p
at the line *p = 5;
Why is that the assignment j = 10; is valid and *p = 5; is an error.
const int *p means that you can't modify the integer that p is pointing to using p, as in *p = 5;. The integer it points to may not be a const int, which is why j = 10 works. This prevents coders from modifying integer being pointed to.
const int* p mean you can not change the content in the address of p
which is you can not chanage *p
const int mean , its value remain same until program ends so you can not change its value.