Re: arrays as function arguments



fkater@xxxxxxxxxxxxxx wrote:
Hi,

why doesn't calling f1 (see below) cause a warning while calling f2
does? Both seem to pass the wrong type. Is there another way to
declare f1 so that the caller is forced (warned) if the passed array
hasn't the same elements? I hope this is related to C and not a
question of the used compiler (here: gcc-4.1.2).

void f1(int a[4]){}
void f2(int (*b)[4]){}

int main(int argc,char** argv){

int c[4+1];
int (*d)[4+1];

f1(c); /* no warning */
f2(d); /* warning: incompatible pointer type */

return 0;
}

Felix


In C, array arguments are equivalent to pointers, so for example:


void somefunc(char array[10])
{}

void somefunc(char array[])
{}

void somefunc(char array[5])
{}

void somefunc(char *array)
{}


are all equivalent.
.



Relevant Pages

  • Re: fgets() - supposed to be simple :)
    ... shouldn't) use the macro with a semicolon. ... And you need an array of 100 floats for the amount of the purchase? ... >void menu; ... This is an error, not a warning. ...
    (comp.lang.c)
  • Re: Stack prob. Setting callback in native dll.
    ... static void dllSetCallFp(void (*aFp)); ... The problem is I can debug into the calling of MFun but when I return I ... with a different calling convention. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Stack prob. Setting callback in native dll.
    ... delegate(i.e., modopt) using ildasm ... static void dllSetCallFp(void (*aFp)); ... The problem is I can debug into the calling of MFun but when I return I ... with a different calling convention. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: print "foo" without using ;
    ... The main reason that an OS (or other calling environment) wouldn't accept a void function, rather than an function returning int is that the calling convention is incompatible. ...
    (comp.lang.c)
  • Re: print "foo" without using ;
    ... but perhaps surprising is the fact that Microsoft ... >> actually accepts void as a valid return type. ... > returning int is that the calling convention is incompatible. ... The standard certainly makes calling a function ...
    (comp.lang.c)