I am very confused with the following thing

#include <stdio.h>
int main(void)
{
int array[3]={1,2,3};
int (*p)[3];
p = &array;
/* Here p contians address of the array */
return 0;
}

Here p contains the adddress of the array. I am very confused why p does not give me the first value that is 1. I know it is very common question but please explain me. I know p is of type int ()[3]. How to proceed step by step to understand accessing of each element in the array.

p is a pointer to an array, not a pointer to an element of the array. You'll need to dereference the pointer, then treat the result like an array:

printf("%d\n", (*p)[1]);

my doubt is from whatever I have understood dereferencing means gives the value at the address pointed by the pointer in both the cases for int p or int ()[] both are pointing to the same address then where is the difference coming in. I am missing something in pointers?

I am missing something in pointers?

Yes. Pointers have both a value and a type. The value is the address you're talking about, but the type says how you can use the pointer without a cast. The type of your pointer is an array type, so it's unreasonable to expect it to be compatible with a non-array type.

After lot of analysis this is my conclusion that if for example if p is pointer to int then when you dereference it will be 4 bytes based on machine similarly if p is a pointer to char then when dereference it will be 1 byte. similarly if p is a pointer to array then when i dereference it will be the first element or the address of the first element and to get the value at the first element you have to dereference it again since it is address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.