U
usr.root
Is there any differece of this two function:
int f1(int first[]);
int f2(int *second);
int f1(int first[]);
int f2(int *second);
Is there any differece of this two function:
int f1(int first[]);
int f2(int *second);
oh,Thank you,but i still do not know why ?
ajm said:hidden in the standard somewhere is a remark to the effect that a
function is unable to distinguish a parameter passed as an array or as
a pointer (since in each case the type is passed by reference, i.e., a
copy is not made of the argument) - this is the only sense in which
array names and pointers are exactly identical.
Is there any differece of this two function:
int f1(int first[]);
int f2(int *second);
ajm said:hidden in the standard somewhere is a remark to the effect that a
function is unable to distinguish a parameter passed as an array or as
a pointer (since in each case the type is passed by reference, i.e., a
copy is not made of the argument) - this is the only sense in which
array names and pointers are exactly identical.
oh,Thank you,but i still do not know why ?
littley said:Not reference, just get the start point of array.
KJ said:As far as i know c doesn't have pass be reference mechanism.
Th above two function prototype are same bcoz internally array is
nothing but manupulated using address (or u can say array name is
constant pointer). So both can be used to manipulate values. The only
thing that reminds me from seeing prototype is that i have to be more
carefull passing an array as parameter bcoz the size has to be taken
care of.
hidden in the standard somewhere is a remark to the effect that a
function is unable to distinguish a parameter passed as an array or as
a pointer (since in each case the type is passed by reference, i.e., a
copy is not made of the argument) - this is the only sense in which
array names and pointers are exactly identical.
Since you can't pass an array to a function it makes no sense to define
a function parameter with array type. However C allows you to do this
(unfortunately?) and what it does is reinterpret the array declarator as
a pointer
I recently came across this explanatory statement by Dennis Ritchie:
"[T]he empty square brackets in the function declaration
int f(a) int a[]; { ... }
are a living fossil, a remnant of NB's way of declaring a pointer; a is,
in this special case only, interpreted in C as a pointer. The notation
survived in part for the sake of compatibility, in part under the
rationalization that it would allow programmers to communicate to their
readers an intent to pass f a pointer generated from an array, rather than
a reference to a single integer. Unfortunately, it serves as much to
confuse the learner as to alert the reader."
yes, unless you're dealing with array of fixed size of elements, e.g.:
#include <stdio.h>
void myfunc (int (*pArr)[4])
{
int i;
for (i=0; i<4; i++)
printf ("%d\n", (*pArr));
}
int main()
{
int arr[4]={1,2,3,4};
myfunc (&arr);
return 0;
}
You'll get a warning if you pass arr instead of &arr to myfunc, but it
should function anyway.
Peter Nilsson said:Alexei said:yes, unless you're dealing with array of fixed size of elements, e.g.:
#include <stdio.h>
void myfunc (int (*pArr)[4])
This isn't an array of fixed size, it's a pointer to an array of
specified size.
{
int i;
for (i=0; i<4; i++)
printf ("%d\n", (*pArr));
}
int main()
{
int arr[4]={1,2,3,4};
myfunc (&arr);
return 0;
}
You'll get a warning if you pass arr instead of &arr to myfunc, but it
should function anyway.
There's a _big_ difference between "you'll get a warning but it should
work anyway" and an implementation refusing to accept the program
because of a constraint violation. When I use arr instead of &arr
I get...
% acc test.c
test.c: In function `main':
test.c:12: error: passing arg 1 of `myfunc' from incompatible pointer
type
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.