Click here to Skip to main content
15,887,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include<stdio.h>
int main()
{
	int a[3]={1,2,3};
	int *p;
	p=&a+2;
	*p=50;

	printf("p = %d\n",p);

	printf("*p = %u\n",*p);

	printf("z = %u\n",&a+1);

	printf("y = %u\n",*(&a+1));

	}


What I have tried:

I am getting the output on particular memory location only by using double pointer. Why I am unable to get the value using single pointer. I am getting the value on *p but unable to get the same value using *(&a+1).kindly help.
Posted
Updated 20-Dec-17 21:11pm
v2

Always compile with the -Wall switch.
As matter of fact GCC warns on
Quote:
p=&a+2;
That shoud be instead
C
p = a + 2;

Try
C
#include <stdio.h>

int main()
{
  int a[3]={1,2,3};
  int *p;
  p=(a+2);
  *p=50;

  int n=0;

  for (n=0; n<3; ++n)
    printf("at address %p value is %d\n", (a+n), *(a+n));

  return 0;
}
 
Share this answer
 
v3
The compiler shows
warning: assignment from incompatible pointer type
for the line
C
p=&a+2;
That is because a is an array and therefore already an int* pointer.

So it must be
C
p = a + 2;
// or
p = &a[2];
and similar in the printf calls.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900