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

H

hn.ft.pris

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.
 
P

p_cricket_guy

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 <stdio.h>
#include <stdlib.h>

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.
 
K

kaili

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;

"


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 <stdio.h>
#include <stdlib.h>

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.
 
K

kaili

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;


"



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 <stdio.h>
#include <stdlib.h>

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.
 
R

Richard Heathfield

(e-mail address removed) said:

int (*p)[3] declares p as a pointer to an array of int of size 3.

example:

#include <stdio.h>
#include <stdlib.h>

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!)
 
S

Simon Biber

Richard said:
(e-mail address removed) said:

int (*p)[3] declares p as a pointer to an array of int of size 3.

example:

#include <stdio.h>
#include <stdlib.h>

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.
 
P

p_cricket_guy

Richard said:
(e-mail address removed) said:

int (*p)[3] declares p as a pointer to an array of int of size 3.

example:

#include <stdio.h>
#include <stdlib.h>

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.
 
J

jacob navia

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 :)
 
R

Richard Heathfield

Simon Biber said:
Richard said:
(e-mail address removed) 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 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.

Oops, you're right. Careless of me.

My apologies to the OP, and to anyone who was misled.
 
R

Richard Heathfield

(e-mail address removed) 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.
 
H

hn.ft.pris

Thanks very much everyone. I get it.
"Richard Heathfield дµÀ£º
"
(e-mail address removed) 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.
 
D

dspfun

A related question:
int *p[3]; has different meaning than int (*p)[3];.

But is int i[3]; any different from int (i)[3]; ?
 
C

Cesar Rabak

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
cdecl> explain int i[3]
declare i as array 3 of int
cdecl> explain int (i)[3]
declare i as array 3 of int
cdecl> explain int (*p)[3]
declare p as pointer to array 3 of int
cdecl> explain int *p[3]
declare p as array 3 of pointer to int
cdecl>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top