Click here to Skip to main content
15,886,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
void main()
{
 int arr[]={1,2,3}; 
printf("%u %u",arr,&arr); 
printf("%u %u",arr+1,&arr+1);
}

first statement i am getting same address for arr and &arr.
2nd it gives different y?
wats the difference in using arr and &arr?
can any one explain me?
Posted
Updated 29-Jun-15 22:54pm
v2

1 solution

arr and &arr always hold the same address (the name of an array is a pointer to array first item, also &arr[0] gives the same address).
As for pointer math, the nature (the type) of arr and &arr is different: (arr+1) jumps to the next (the 2nd) item of the array, while (&arr+1) jumps to the end of the array (the compiler takes into account the array size).

Please note your code should be:
C
#include <stdio.h>

int main()
{
  int arr[]={1,2,3};
  printf("%p %p\n",arr,&arr);
  printf("%p %p\n",arr+1,&arr+1);
  return 0;
}



Quote:
can you say me the use of **p? . its confusing. thankz in advance.

A double pointer could be used, for instance, in jagged array implementation:
C
 #include <stdio.h>
 #include <stdlib.h>
 #define N 3
int main()
{
  int ** pp;
  int size[N] = { 2,3,1 } ;
  pp = (int**) malloc( N * sizeof(int *));

  int n,k, count=0;

  for (n=0; n<N; ++n)
  {
    pp[n] =(int *)  malloc( size[n] * sizeof(int));
    for (k=0; k<size[n]; ++k, ++count)
      pp[n][k] = count;
  }

  for (n=0; n<N; ++n)
  {
    for (k=0; k<size[n]; ++k)
      printf("pp[%d][%d] = %d\n", n, k, pp[n][k]);
    printf("-------------\n");
  }


  for (n=0; n<N; ++n)
  {
    free(pp[n]);
  }
  free (pp);

  return 0;
}
 
Share this answer
 
v4
Comments
moyna coder 30-Jun-15 4:56am    
I got it.. thank you.
CPallini 30-Jun-15 5:00am    
You are welcome.
moyna coder 30-Jun-15 5:08am    
why cant it be %u? what is wrong in that?
CPallini 30-Jun-15 5:26am    
Well if you need to printout a pointer the %p is the proper format specifier. It is not granted that an unsigned int can hold an address (usually it can).
moyna coder 30-Jun-15 6:12am    
can you say me the use of **p? . its confusing. thankz in advance.

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