calling function with 2-dim arrays

J

jeremy targett

Hello, in my program I use 2-dimensional arrays to hold two integer values
for each of i members of a list. array[0] holds a starting
position, and array[1] holds a label - in fact I #defined START as 0
and LABEL as 1 so I can just write

array[START] = whatever
array[LABEL] = whatever

(I declared array like this:

int array[MAXLENGTH][2];
)

I want to compare two arrays to see which is "best", first by comparing
the START values (at the first difference, the lower one is better), then
the LABEL values for each member (same rule). So I made a function
"arraycmp" which I invoke with the two arrays and a length. The function
should return <0 if the first array is better, >0 if the second array is
better, and 0 if they are the same. Here's my function:

arraycmp(int T[][], int S[][], int len)
{
int i;
for (i=0;i<len;i++)
if (T[START] != S[START])
return (T[START]-S[START]);
for (i=0;i<len;i++)
if (T[LABEL] != S[LABEL])
return (T[LABEL]-S[LABEL]);
return 0;
}

When I compile my program, gcc says this:

invalid use of array with unspecified bounds

Could someone tell me how to fix this please? Many thanks --Jeremy
 
P

Peter Nilsson

jeremy said:
Hello, in my program I use 2-dimensional arrays to hold two integer
values for each of i members of a list. array[0] holds a starting
position, and array[1] holds a label - in fact I #defined START
as 0 and LABEL as 1 so I can just write

array[START] = whatever
array[LABEL] = whatever

(I declared array like this:

int array[MAXLENGTH][2];
)

I want to compare two arrays to see which is "best", first by comparing
the START values (at the first difference, the lower one is better),
then the LABEL values for each member (same rule). So I made a function
"arraycmp" which I invoke with the two arrays and a length. The function
should return <0 if the first array is better, >0 if the second array
is better, and 0 if they are the same. Here's my function:

arraycmp(int T[][], int S[][], int len)
{
int i;
for (i=0;i<len;i++)
if (T[START] != S[START])
return (T[START]-S[START]);
for (i=0;i<len;i++)
if (T[LABEL] != S[LABEL])
return (T[LABEL]-S[LABEL]);
return 0;
}

When I compile my program, gcc says this:

invalid use of array with unspecified bounds


Try...

int arraycmp(int T[][2], int S[][2], int len)

Arrays are just sequences. The language semantics don't require that
arrays be passed with hidden info such as it's dimension. In fact, the
language doesn't allow arrays to be passed at all. Instead, arrays
generally decay to pointers when used within expression (although
there are exceptions like when they are applied to & or sizeof.)

Whilst you can write a declaration like...

int foo(X somearray[])

....this is implicitly treated as...

int foo(X *somearray);

So you're not really passing an array, rather you're passing a
pointer to the first element.

This applies even if X itself is an array, but it _only_ applies to the
first dimension of an array parameter.

In your sample function declaration, consider what the statement
T[1] is meant to mean. It means the second array of ints in the
sequence, but since you haven't told the compiler how many elements
there are in that sequence (in this case 2), how is the compiler
to determine where the first sequence ends and the next one begins?

If you supply the [2], then compiler knows that X[1] is two ints
from the beginning of X and it can proceed.
 
L

lawrence.jones

jeremy targett said:
arraycmp(int T[][], int S[][], int len)

You're only allowed to leave the first dimension unspecified. So,
change that to:

arraycmp(int T[][2], int S[][2], int len)

and you'll be in business.

-Larry Jones

It's going to be a long year. -- Calvin
 
E

Emmanuel Delahaye

jeremy targett wrote on 30/07/05 :
int array[MAXLENGTH][2];

arraycmp(int T[][], int S[][], int len)
{


int arraycmp(int T[MAXLENGTH][2], int S[MAXLENGTH][2], size_t len)
{

or

int arraycmp(int T[][2], int S[][2], size_t len)
{

Note that the return type has to be explicit with C99. Also that the
correct type for a size is ... size_t!

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
J

jeremy targett

Peter Nilsson said:
jeremy targett wrote: [snip]
arraycmp(int T[][], int S[][], int len) [snip]
invalid use of array with unspecified bounds

Try...

int arraycmp(int T[][2], int S[][2], int len)

Thanks, Peter, for the great explanation - it made perfect sense. Thanks
Lawrence also, and anyone else who answered.

--Jeremy
 
J

Jasen Betts

Hello, in my program I use 2-dimensional arrays to hold two integer values
for each of i members of a list. array[0] holds a starting
position, and array[1] holds a label - in fact I #defined START as 0
and LABEL as 1 so I can just write

array[START] = whatever
array[LABEL] = whatever

(I declared array like this:

int array[MAXLENGTH][2];
)

I want to compare two arrays to see which is "best", first by comparing
the START values (at the first difference, the lower one is better), then
the LABEL values for each member (same rule). So I made a function
"arraycmp" which I invoke with the two arrays and a length. The function
should return <0 if the first array is better, >0 if the second array is
better, and 0 if they are the same. Here's my function:

arraycmp(int T[][], int S[][], int len)
{
int i;
for (i=0;i<len;i++)
if (T[START] != S[START])
return (T[START]-S[START]);
for (i=0;i<len;i++)
if (T[LABEL] != S[LABEL])
return (T[LABEL]-S[LABEL]);
return 0;
}

When I compile my program, gcc says this:

invalid use of array with unspecified bounds

Could someone tell me how to fix this please? Many thanks --Jeremy


to make it compile (and probably work) change the definition of the
function to

arraycmp(int T[][2], int S[][2], int len)


but you'd probably be better off doing it this way

typedef struct {int label,start} thingtype;
thingtype array [MAXLENRTH]
//...//
arraycmp(thingtype T[], thingtype S[][], int len)
{
int i;
for (i=0;i<len;i++)
if (T.start != S.start)
return (T[START]-S[START]);
for (i=0;i<len;i++)
if (T.label != S.label)
return (T.label-S.label);
return 0;
}

On the other hand memcmp(T,S,sizeof(int[2]) * len) could be used if
equality/inequality is the only significance of the result.
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top