454,137 Members | 1,024 Online
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

What does "int (*p)[3]" mean and how to use it?

 P: n/a I know int *p[3] mean an array of pointers to int, but I meet int (*p)[3] which is confusing. What does it mean and how to use it? Thanks for help. Jan 1 '07 #1
12 Replies

 P: n/a hn********@gmail.com wrote: I know int *p[3] mean an array of pointers to int, but I meet int (*p)[3] which is confusing. What does it mean and how to use it? Thanks for help. int (*p)[3] declares p as a pointer to an array of int of size 3. example: #include #include int main (void) { int (*p)[3]; int foo[3] = {0, 1, 2}; p = &foo; printf(" %d -- %d -- %d\n", (*p)[0], (*p)[1], (*p)[2]); return EXIT_SUCCESS; } PS: Please look into the FAQ for better explanation. Jan 1 '07 #2

 P: n/a is (*p)[?] equals to foo[?] here? i can use (*p)[?] anywhere i used foo[?] ? i mean can i replace all foo[?] with (*p)[?] as lang as i declared " int (*p)[3]; int foo[3] = {0, 1, 2}; p = &foo; " p_***********@yahoo.co.in wrote: hn********@gmail.com wrote: I know int *p[3] mean an array of pointers to int, but I meet int (*p)[3] which is confusing. What does it mean and how to use it? Thanks for help. int (*p)[3] declares p as a pointer to an array of int of size 3. example: #include #include int main (void) { int (*p)[3]; int foo[3] = {0, 1, 2}; p = &foo; printf(" %d -- %d -- %d\n", (*p)[0], (*p)[1], (*p)[2]); return EXIT_SUCCESS; } PS: Please look into the FAQ for better explanation. Jan 1 '07 #3

 P: n/a p_***********@yahoo.co.in said: > int (*p)[3] declares p as a pointer to an array of int of size 3. example: #include #include int main (void) { int (*p)[3]; int foo[3] = {0, 1, 2}; p = &foo; So far so good... printf(" %d -- %d -- %d\n", (*p)[0], (*p)[1], (*p)[2]); printf can print void pointer values if you use %p, but that's it. So the above line should read: printf(" %p -- %p -- %p\n", (void *)(*p)[0], (void *)(*p)[1], (void *)(*p)[2]); (Format to taste!) -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Jan 1 '07 #4

 P: n/a is (*p)[?] equals to foo[?] here? i can use (*p)[?] anywhere i used foo[?] ? i mean can i replace all foo[?] with (*p)[?] as lang as i stated " int (*p)[3]; int foo[3] = {0, 1, 2}; p = &foo; " p_***********@yahoo.co.in wrote: hn********@gmail.com wrote: I know int *p[3] mean an array of pointers to int, but I meet int (*p)[3] which is confusing. What does it mean and how to use it? Thanks for help. int (*p)[3] declares p as a pointer to an array of int of size 3. example: #include #include int main (void) { int (*p)[3]; int foo[3] = {0, 1, 2}; p = &foo; printf(" %d -- %d -- %d\n", (*p)[0], (*p)[1], (*p)[2]); return EXIT_SUCCESS; } PS: Please look into the FAQ for better explanation. Jan 1 '07 #5

 P: n/a Richard Heathfield wrote: p_***********@yahoo.co.in said: int (*p)[3] declares p as a pointer to an array of int of size 3. example: #include #include int main (void) { int (*p)[3]; int foo[3] = {0, 1, 2}; p = &foo; So far so good... printf(" %d -- %d -- %d\n", (*p)[0], (*p)[1], (*p)[2]); printf can print void pointer values if you use %p, but that's it. So the above line should read: printf(" %p -- %p -- %p\n", (void *)(*p)[0], (void *)(*p)[1], (void *)(*p)[2]); Isn't (*p)[x] an int? Why do we need to print it as a pointer? I may be missing something and unable to find out what it is. Thanks. > (Format to taste!) -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Jan 1 '07 #6

 P: n/a Richard Heathfield wrote: p_***********@yahoo.co.in said: >int (*p)[3] declares p as a pointer to an array of int of size 3.example:#include #include int main (void){ int (*p)[3]; int foo[3] = {0, 1, 2}; p = &foo; So far so good... > printf(" %d -- %d -- %d\n", (*p)[0], (*p)[1], (*p)[2]); printf can print void pointer values if you use %p, but that's it. So the above line should read: Read the code again. It is not printing void pointer values. It is printing int values. The code is perfectly correct. p is pointer to array 3 of int (*p) is array 3 of int, decays to pointer to int (*p)[0] is int printf(" %p -- %p -- %p\n", (void *)(*p)[0], (void *)(*p)[1], (void *)(*p)[2]); Now this is wrong -- converting ints to void pointers is not a good idea. -- Simon. Jan 1 '07 #7

 P: n/a Simon Biber a écrit : > Read the code again. It is not printing void pointer values. It is printing int values. The code is perfectly correct. p is pointer to array 3 of int (*p) is array 3 of int, decays to pointer to int (*p)[0] is int > printf(" %p -- %p -- %p\n", (void *)(*p)[0], (void *)(*p)[1], (void *)(*p)[2]); Now this is wrong -- converting ints to void pointers is not a good idea. Now, it is the 1st of January in the morning... Many people have a hangover :-) Jan 1 '07 #8

 P: n/a Simon Biber said: Richard Heathfield wrote: >p_***********@yahoo.co.in said: >> >> printf(" %d -- %d -- %d\n", (*p)[0], (*p)[1], (*p)[2]); printf can print void pointer values if you use %p, but that's it. So theabove line should read: Read the code again. It is not printing void pointer values. It is printing int values. The code is perfectly correct. Oops, you're right. Careless of me. My apologies to the OP, and to anyone who was misled. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Jan 1 '07 #9

 P: n/a p_***********@yahoo.co.in said: Isn't (*p)[x] an int? Why do we need to print it as a pointer? I may be missing something and unable to find out what it is. No, you're not missing anything - I am. Sorry about that. I completely misread your printf. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Jan 1 '07 #10

 P: n/a Thanks very much everyone. I get it. "Richard Heathfield Ð´µÀ£º " p_***********@yahoo.co.in said: Isn't (*p)[x] an int? Why do we need to print it as a pointer? I may be missing something and unable to find out what it is. No, you're not missing anything - I am. Sorry about that. I completely misread your printf. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Jan 1 '07 #11

 P: n/a A related question: int *p[3]; has different meaning than int (*p)[3];. But is int i[3]; any different from int (i)[3]; ? Jan 1 '07 #12

 P: n/a dspfun escreveu: A related question: int *p[3]; has different meaning than int (*p)[3];. But is int i[3]; any different from int (i)[3]; ? Consider obtaining a functioning 'cdecl' utility for the platform you use: \$ cdecl Type `help' or `?' for help cdeclexplain int i[3] declare i as array 3 of int cdeclexplain int (i)[3] declare i as array 3 of int cdeclexplain int (*p)[3] declare p as pointer to array 3 of int cdeclexplain int *p[3] declare p as array 3 of pointer to int cdecl> Jan 1 '07 #13