Array Arguments.....

P

prashna

Hi all,
Is there any difference between the following 2 function prototypes
which accepts an array as argument?

void foo(int arr[]);
void foo(int *arr);

Which of the 2 is the best method of declaring a function prototype
which has array as arguments?

Thanks
 
E

Eric Sosman

prashna said:
Hi all,
Is there any difference between the following 2 function prototypes
which accepts an array as argument?

void foo(int arr[]);
void foo(int *arr);

This is Question 6.4 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
Which of the 2 is the best method of declaring a function prototype
which has array as arguments?

Which is better: A pineapple or a pomegranate?
 
A

Andrey Tarasevich

prashna said:
Is there any difference between the following 2 function prototypes
which accepts an array as argument?

void foo(int arr[]);
void foo(int *arr);

No.

It might be worth noting that in C89/C90 with the first didn't let you
to declare the pointer itself as 'const', i.e. declaration

void foo(int *const arr);

didn't have an equivalent in the first format. However, C99 removed this
limitation - declaration

void foo(int arr[const]);

is equivalent to the previous one. (Assuming that you care about
declaring function parameters as 'const'. I don't remember ever doing it
myself.)
Which of the 2 is the best method of declaring a function prototype
which has array as arguments?

They are equivalent, which means that this is a matter of personal
preferences and/or coding standards. There's no "best" here.
 
M

Method Man

It might be worth noting that in C89/C90 with the first didn't let you
to declare the pointer itself as 'const', i.e. declaration

void foo(int *const arr);

didn't have an equivalent in the first format. However, C99 removed this
limitation - declaration

void foo(int arr[const]);

is equivalent to the previous one.

This is a horrible equivalence IMO. They should have stuck with the first
declaration instead of introducing new confusing syntax.
 
M

Michael Mair

Method said:
It might be worth noting that in C89/C90 with the first didn't let you
to declare the pointer itself as 'const', i.e. declaration

void foo(int *const arr);

didn't have an equivalent in the first format. However, C99 removed this
limitation - declaration

void foo(int arr[const]);

is equivalent to the previous one.

This is a horrible equivalence IMO. They should have stuck with the first
declaration instead of introducing new confusing syntax.

I guess they did it only as they already had to do it for the
static keyword ( "int foo (int bar[static 3])" tells you that
you can safely access bar[0] through bar[2] ).

-Michael
 
C

Charlie Gordon

Michael Mair said:
Method said:
It might be worth noting that in C89/C90 with the first didn't let you
to declare the pointer itself as 'const', i.e. declaration

void foo(int *const arr);

didn't have an equivalent in the first format. However, C99 removed this
limitation - declaration

void foo(int arr[const]);

is equivalent to the previous one.

This is a horrible equivalence IMO. They should have stuck with the first
declaration instead of introducing new confusing syntax.

I guess they did it only as they already had to do it for the
static keyword ( "int foo (int bar[static 3])" tells you that
you can safely access bar[0] through bar[2] ).

Was this syntax discussed in the ISO meetings ?

int[3] bar
int foo(int[3] bar)
 

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,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top