Click here to Skip to main content
15,887,472 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<stdio.h>

int main()
{

int var[5]={10,20,30,40};
int(*ptr)[5];//array of pointer.

ptr=&var;
printf("Address of var : %u\n",&var);
printf("Value of var[0] is : %d\n",*(&var[0]));
printf("Value inside ptr after ptr=&var : %u\n",ptr);
printf("Value of ptr[0] is : %u\n",*(&ptr[0]));
printf("Adress of ptr[0] is : %u\n",&ptr[0]);

printf("Value of ptr[1] is : %u\n",*(&ptr[1]));
printf("Adress of ptr[1] is : %u\n",&ptr[1]);

printf("Value of ptr[2] is : %u\n",*(&ptr[2]));
printf("Adress of ptr[2] is : %u\n",&ptr[2]);
}
Posted
Comments
Member 11372005 9-Jul-15 5:43am    
Also how to get rid of these below warnings.

inr_ptr.c: In function ‘main’:
inr_ptr.c:10:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int (*)[5]’ [-Wformat=]
printf("Address of var : %u\n",&var);
^
inr_ptr.c:12:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int (*)[5]’ [-Wformat=]
printf("Value inside ptr after ptr=&var : %u\n",ptr);
^
inr_ptr.c:13:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("Value of ptr[0] is : %u\n",*(&ptr[0]));
^
inr_ptr.c:14:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int (*)[5]’ [-Wformat=]
printf("Adress of ptr[0] is : %u\n",&ptr[0]);
^
inr_ptr.c:16:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("Value of ptr[1] is : %u\n",*(&ptr[1]));
^
inr_ptr.c:17:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int (*)[5]’ [-Wformat=]
printf("Adress of ptr[1] is : %u\n",&ptr[1]);
^
inr_ptr.c:19:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("Value of ptr[2] is : %u\n",*(&ptr[2]));
^
inr_ptr.c:20:1: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int (*)[5]’ [-Wformat=]
printf("Adress of ptr[2] is : %u\n",&ptr[2]);
^

As you have already understood by yourself your declaration is wrong. But doesn't seem that you understood also why it is wrong.
The point is that you have not declared an array of pointers, but a pointer to an array of integers!
The use of brackets emphasized the pointer nature of the declared variable 'ptr'.
Please see the following code that shows the declarations of:
1. An array of pointers to int
2. A pointer to an array of int
3. A pointer to an array of pointers to int
The code shows also the initialization of them.
C++
//Case array of pointers to int
int *ptr[5];    //ptr is an array of 5 pointers to int
int var=100;
int var1 = 1, var2=2, var3=3, var4=4;
ptr[0]=&var;
ptr[1]=&var1;
ptr[2]=&var2;
ptr[3]=&var3;
ptr[4]=&var4;

//case pointer to array of integers
int (*ptr1)[5]; //ptr is a pointer to an array of 5 integers.
int arr[5]={10,20,30,40,50};        //array of integers
ptr1 = &arr;

//case pointer to array of pointers to integer
int *(*ptr2)[5];  //ptr is a pointer to an array of 5 pointers to int
ptr2 = &ptr;

Now back to your code there are many errors, because if your intent was to declare 'ptr' as an array of pointers to int you cannot assign to it the address of an int.
Any decent compiler should have throw a bunch of errors an a lot of warnings. Have you read them carefully?
 
Share this answer
 
v5
Change below code

C++
printf("Value of ptr[1] is : %u\n",*(&ptr[1]));
printf("Adress of ptr[1] is : %u\n",&ptr[1]);


with this

C++
printf("Value of ptr[1] is : %u\n",*(ptr[1]);
printf("Adress of ptr[1] is : %u\n",ptr[1]);


and see yourself what you were doing wrong! :)
 
Share this answer
 
Comments
Member 11372005 9-Jul-15 6:58am    
Ok in the below code, address of var[0] will get stored in the ptr[0]. what is address or location of ptr[0] in the memory. For example here, value of var[0] is 10 , but its address is i.e &var[0] is some dummy number.

#include<stdio.h>

int main()
{

int var[5]={10,20,30,40};
int(*ptr)[5];//array of pointer.

ptr=&var[0];
printf("Value of var[0] is : %d\n",var[0]);
printf("Address of var : %u\n",&var[0]);
printf("Value inside ptr after ptr=&var : %u\n",ptr[0]);
printf("Value of ptr[0] is : %d\n", ptr[0]);
printf("Adress of ptr[0] is : %u\n",&ptr[0]);

printf("Value of ptr[1] is : %d\n",ptr[1]);
printf("Adress of ptr[1] is : %u\n",&ptr[1]);

printf("Value of ptr[2] is : %d\n",ptr[2]);
printf("Adress of ptr[2] is : %u\n",&ptr[2]);
//ptr=ptr+1;
//printf("Value inside ptr after ptr=ptr+1 is : %u\n",ptr);

return 0;
}
Member 11372005 9-Jul-15 7:26am    
when i declared this int(*ptr)[5]; whether memory will be allocated ?
Member 11372005 9-Jul-15 7:40am    
Ok got it. int(*ptr)[5]; doesn't allocate any memory , and my above statement is wrong i.e "address of var[0] will get stored in the ptr[0]. " instead address of var[0] will be stored in ptr.
Array name can be used as a pointer in some cases. You might look this: http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c[^].

And since printf is declared with ... for arguments, there is no validation at compile time nor any conversion and thus you have to know how it works.

In summary, array and &array are synonym in that context. An array is already a pointer.
 
Share this answer
 
Comments
Frankie-C 11-Jul-15 11:20am    
Yes, you're right, but, as stated also in the link provided, is not completely exact. An array name is equivalent to the address of the first element of the array, so it's a pointer to the element type. This small difference makes the trick when assigning values.
I.e. in the examples in my answer if you replace:
ptr1 = &arr;
with:
ptr1 = arr;
You will get an error from compiler, at least if it is ANSI compliant, like this:
error #2168: Operands of '=' have incompatible types 'int (*)[5]' and 'int *'
You can solve it with a cast:
(int *)ptr1 = arr;
But it's much more elegant, and correct, to use the right form.
As already mentioned in other solutions, your definition of the array is not what you need.
E.g.
C
int (*p)[5]; // declare p as pointer to array 5 of            int
int  *p [5]; // declare p as            array 5 of pointer to int

To play around with the definition of arrays, see http://www.cdecl.org/[^].
Regards
Andi
 
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