who can tell me the differece of this two ?

U

usr.root

Is there any differece of this two function:

int f1(int first[]);
int f2(int *second);
 
P

pete

oh,Thank you,but i still do not know why ?

Types of functions are according to the return type
and the type of the parameters.
They both return type int.
They each have one parameter of type pointer to int.
 
A

ajm

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.

hth,
ajm.
 
A

Alexei A. Frounze

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.

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.

Alex
 
K

KJ

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

John Bode

Is there any differece of this two function:

int f1(int first[]);
int f2(int *second);

There is no difference. Both declarations are of functions that return
an int take a single parameter of pointer to int (in this context, int
foo[] is synonymous with int *foo; note that this is the *only* context
where that is true).
 
K

Keith Thompson

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.

========================================================================
ATTENTION ALL GROUPS.GOOGLE.COM USERS:
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
========================================================================

For information about arrays and pointers, see section 6 of the C FAQ,
imaginatively titled "Arrays and Pointers".
 
D

Default User

oh,Thank you,but i still do not know why ?

Why what? Please quote enough of the previous message for context. To
do so from Google, click "show options" and use the Reply shown in the
expanded header.




Brian
 
D

Default User

littley said:
Not reference, just get the start point of array.


Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.




Brian
 
D

Default User

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.

Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.




Brian
 
L

Lawrence Kirby

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.

Not exactly.

In C you can NEVER pass an array as a function argument, the language
simply doesn't support it. COnsider

int a[10];
int *p = a;

This is a typical case of an array being converted to a pointer to its
first element when you take its "value" as is happening in the
initialisation of p. Function argument passing is a lot like
initialisation, the caller supplies a value which is used to initialise
the parameter variable in the called function. When you try to pass an
array the value that gets passed is a pointer to the first element of the
array, i.e. the same vale as you get in most other contexts like
initialisation or assignment.

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.e. changes it in a way that corresponds to the conversion from
array to pointer that happens in the caller. So

void foo(int a[10])
{
size_t sizea = sizeof a;
a = NULL;
}

the type of a here really is int * and not int [10], so for example sizea
would be set to sizeof(int *) and NOT sizeof(int [10]). Also assigning
NULL to a works here, which would be invalid if a was actually an array.
Note that that changing of the declared type of an object from array to
pointer ONLY happens for a function parameter, nowhere else.

Lawrence
 
N

Netocrat

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

The Development of the C Language
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html
 
A

Alexei A. Frounze

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

Great. And when I attempted to make it even more clear to the reader and
made it as
int f(int(*a)[4])
to show that this array must have 4 elements inside, I got into a bigger
trouble since the type of a is neither pointer to a single element nor some
simple type as it exhibits additional indirection :)
As you remember, we've discussed this here...

Alex
 
P

Peter Nilsson

No. C passes all parameters by value.

"The Rule" to learn is that an array will decay to a pointer to the
first element when it's used in an expression and it is not an
operand of sizeof or unary &.

You're not naming an array when you declare a function parameter as
an array declaration. It is implicitly treated as a pointer to the
type of the array element. That element may itself be an array. If so,
that array type is unchanged...

void foo(int a[10][20]);
void foo(int (*a)[20]);
void foo(int a[][20]);
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
 
A

Alexei A. Frounze

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.

I beg your pardon for speaking poor English, but I meant essentially the
same thing, just a bad word match. :)
{
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


Then that's compiler specific (or invokation specific). Other than that, if
it compiles without an error, it should work. But I really do agree that any
code pretending to be more or less portable with minimum of undefined
behavior etc etc, it must at least compile w/o any warnings (with all
enabled).

Alex
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top