Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use int (*p)[3] 2

Status
Not open for further replies.

yogesh77

Programmer
Jul 19, 2001
18
US
what is int (*p)[3] and what is it
 
in my opinion is an array of pointers to function. John Fill
1c.bmp


ivfmd@mail.md
 
No way. It is not an ARRAY, it is a pointer to array of 3 integer.
You can allocate an array where each element will be array of 3 integer. This way you can allocate 2 dim array by one call to malloc (second dimension will be fixed).

int (*p)[3] = NULL;
p=malloc (10 * sizeof (int[3]));
will allocate array 10x3.
p[9][2] = 1;
Very useful if you are going to store array of string with fixed lenght, XYZ coordinates of points, etc.

Or even like that:
int (*p)[2][3][5] = NULL; :)
It is going to be 4 dim array.....
 
int (*p)[3] = NULL; pointer to array of 3 integer.
int *p[3] = {NULL,NULL,NULL}; array of 3 pointers to int.
BIG DIFFERENCE.
 
int (*p)[3]
p is a pointr to array of size 3.
p--> |2|5|6|1|
>> here one advantage of having pointer to array is that u can change the address of p pointing to some other array of int. but in general
int p[3]
u cant change the address.
 
Okay folks. Here goes:

int *p; pointer to int
int *p[10]; array[10] of pointers to int (not same as a 2D array)
int (*p)[10]; pointer to array[10] of int
int *p(); function returning pointer to int
int (*p)(); pointer to function returning int
int *(*p)(); pointer to function returning pointer to int
int (*p[])(); array[] of pointers to function returning int
int (*(*p())[5])(); function returning pointer to array[5] of pointers to function returning int

If you find yourself confused, believe me you are not the first in the world. But go through the above again keeping in mind the precedence and associativity of operators:
() [] {left to right}
* {right to left}

Finally if you can tell what the following is:
int *((*(*p[5])()))[10]
you can assume safely that you are a lot less confused.X-)

Bye.
Ankan. Please do correct me if I am wrong. :)
 
In case you were wondering, the declaration
int *((*(*p[5])()))[10];
makes p an array[5] of pointers to function returning pointer to array[10] of pointers to int.

Hope we all agree :).
In case you have a different answer, or if you want a nice and easy way to come to the above correct conclusion go to faq205-924.

Bye.
Ankan.
Please do correct me if I am wrong. :)
 
One more point.

The (*p)[3] can also be used while passing a two dimensional array to a function. Like

int main()
{
int a[10][3];
...
func(a, 10);
}

void func(int (*p)[3], int nElements)
{
...
}



Maniraja S
 
Hi!
Can anybody tell me how to use
int *p[10];
I have problems giving it a pointer like
*p[0] = ...;
Thanks for your help!
 
int *p[10]
declares p as an array[10] of pointers to int. A simple example of use is...

int *p[10];
int num;

p[3] = # /* makes the address of num the 4th element of p */
[color]
Similarly...:) Ankan.

Please do correct me if I am wrong. s-)
 
Its correct.
To simply int *p[10] means that you have 10 pointers pointing to integers. Since each of these are pointers (like p[0], p[1], etc), they can in turn point to a single number or an array of numbers.

Srik
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top