pointer to arrays as parameter

M

Michael Birkmose

The following creates a function "test" that takes a parameter that is
a pointer to a multi dimensional array.

typedef int int_arr[][3];

void test(int_arr *foo) {
printf("%d\n", (*foo)[1][2]);
}

int main() {
int a[2][3]= {1,2,3,4,5,6};
test(&a);
}

However, how do I specify this, if I don't want to use the typedef?

E.g what I would LIKE to do is:
void test(int[][3] *foo) {...}

However this produces a parse error. Is there any way to avoid typedefing?
 
A

Arthur J. O'Dwyer

The following creates a function "test" that takes a parameter that is
a pointer to a multi dimensional array.

typedef int int_arr[][3];

void test(int_arr *foo) {
printf("%d\n", (*foo)[1][2]);
}

However, how do I specify this, if I don't want to use the typedef?

This is Rule Number One of C: Declaration mimics use.
You wrote (*foo)[x][y] to use 'foo'; now write (*foo)[x][y] to
declare 'foo'. And since (*foo)[x][y] is an 'int', stick an
'int' in front so the compiler knows what type it is.

void test(int (*foo)[][3]) {
printf("%d\n", (*foo)[1][2]);
}

Simple, huh?

-Arthur
 
E

Eric Sosman

Michael said:
The following creates a function "test" that takes a parameter that is
a pointer to a multi dimensional array.

typedef int int_arr[][3];

void test(int_arr *foo) {
printf("%d\n", (*foo)[1][2]);
}

int main() {
int a[2][3]= {1,2,3,4,5,6};
test(&a);
}

However, how do I specify this, if I don't want to use the typedef?

E.g what I would LIKE to do is:
void test(int[][3] *foo) {...}

However this produces a parse error. Is there any way to avoid typedefing?

void test(int (*foo)[][3]) { ... }

Are you sure this is what you want to do, though? The
extra level of indirection doesn't seem to be necessary, and
you could instead write

void test(int foo[][3]) { ... }

and

test(foo);

without the `&' operator.
 
B

Ben Pfaff

Michael Birkmose said:
typedef int int_arr[][3];

void test(int_arr *foo) {
printf("%d\n", (*foo)[1][2]);
}

However, how do I specify this, if I don't want to use the typedef?

You should get a copy of the cdecl program. Example session:

blp@blp:~(0)$ cdecl
Type `help' or `?' for help
cdecl> declare pointer to array of array 3 of int
int (*var)[][3]
cdecl> quit
blp@blp:~(0)$
 
J

Joe Wright

Ben said:
typedef int int_arr[][3];

void test(int_arr *foo) {
printf("%d\n", (*foo)[1][2]);
}

However, how do I specify this, if I don't want to use the typedef?


You should get a copy of the cdecl program. Example session:

blp@blp:~(0)$ cdecl
Type `help' or `?' for help
cdecl> declare pointer to array of array 3 of int
int (*var)[][3]
cdecl> quit
blp@blp:~(0)$

Ben, where can I find cdecl.c source? I have my own versions of decl
and undecl from K&R2 examples but not a complete program. Please.
 
B

Ben Pfaff

Joe Wright said:
Ben, where can I find cdecl.c source? I have my own versions of decl
and undecl from K&R2 examples but not a complete program. Please.

Google for cdecl, or for cdecl.tar.gz. I note that one of the
hits lists "Chris Torek" as submitter--in 1986.
 
C

Chris Torek

Google for cdecl, or for cdecl.tar.gz. I note that one of the
hits lists "Chris Torek" as submitter--in 1986.

I will note here that I did not write it, but I did some minor work
on a version of it at some point, presumably shortly before submitting
it in 1986. :)
 

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
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top