Reputation: 11
I am doing a project for school and am trying to learn how to use pointers. I am trying to get them to relay the value of X, Y, and Z. However, I keep getting the wrong value. X is supposed to equal 1. Y is supposed to equal 2. Z is supposed to equal 3.
{
// Add the C statement(s) necessary to accomplish the task identified in the comments below
double A[3] = {2.718, 3.14, 2.718}; // declare an array A of 3 doubles
A[1] = 3.14; // put the value 3.14 in the middle location of A
A[0] = 2.718;
A[2] = 2.718; // put the value 2.718 in the first and last locations of A
printf("The first value of A is %f\n",A[0]); // print the first value in A
printf("The last value of A is %f\n",A[2]); // print the last value in A
A[2] = A[0] + A[1]; // change the last value in A so that it is the sum of the first two and then print it
printf("The last value of A is %f\n", A[2]);
int B[4] = {10, 25, 50, 100}; // declare an array B of 4 integers with initial values 10, 25, 50, 100
int sum = B[0] + B[1] + B[2] + B[3];
printf("The sum of all four elements in B is %d\n",sum); // print the sum of all four elements in B
printf("Four elements in B in reverse order %d %d %d %d\n",B[3], B[2], B[1], B[0]); // print the four elements in B in reverse order (100, 50, 25, 10)
int X = 1;
int Y = 2;
int Z = 3;
printf("%d, %d, %d\n,",X,Y,Z); // declare three integers, X Y and Z, assign them the values 1, 2 and 3 and print them
int *P1; // declare three pointers to integers, P1, P2 and P3
int *P2;
int *P3;
P1 = &X;
P2 = &Y;
P3 = &Z; // point P1 to X, P2 to Y, and P3 to Z.
printf("%d, %d, %d\n",P1,P2,P3); // print the values at X and Y and Z using the pointers P1 to P3
*P1 = 10;
printf("%d", P1); // using P1 and not X, change the variable X's value from 1 to 10, then print it
return 0;
}
Upvotes: 0
Views: 335
Reputation: 4877
A pointer is a variable that holds the address of another variable.
A pointer to int int *p
will hold the address of a variable of type int.
The operator &
get the address of a variable and the operator *
gets the value stored at an address.
So:
int x = 1; //An integer i=1
int *p = &x; // a pointer to int p to which we assign the address of x
//Now suppose x memory address is 100
printf("int=%d, pointer=%d\n", *p, p); //Will print int=1, pointer=100
The value stored at address pointed by p
is retrieved using the operator *
and shows its value 1
, but if you put the contents of pointer variable p
you get 100
.
But you can't use %d
to print a pointer, you must use %p
.
I used it in printf
only to demonstrate the problem.
Upvotes: 0
Reputation: 539
&X
set the Address of X
to P1
.
but,if you want to access the value stored at that address then you should you *P1
.
What does *P1
means? It means value stored at this address.
P1=&X; //P1 takes address value of X;
and if you want to print value at that address then
printf("Value of X %d\n",*P1);
Upvotes: 1
Reputation: 310930
To access values pointed to by the pointers write
printf("%d, %d, %d\n",*P1,*P2,*P3);
^^^^^^^^^^^^
and
printf("%d", *P1);
^^^^^
the same way as you already wrote
*P1 = 10;
^^^^^^^^
Upvotes: 0