473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

calling function with 2-dim arrays

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

array[i][START] = whatever
array[i][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[i][START] != S[i][START])
return (T[i][START]-S[i][START]);
for (i=0;i<len;i++)
if (T[i][LABEL] != S[i][LABEL])
return (T[i][LABEL]-S[i][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
Nov 15 '05 #1
5 1607
jeremy targett wrote:
Hello, in my program I use 2-dimensional arrays to hold two integer
values for each of i members of a list. array[i][0] holds a starting
position, and array[i][1] holds a label - in fact I #defined START
as 0 and LABEL as 1 so I can just write

array[i][START] = whatever
array[i][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[i][START] != S[i][START])
return (T[i][START]-S[i][START]);
for (i=0;i<len;i++)
if (T[i][LABEL] != S[i][LABEL])
return (T[i][LABEL]-S[i][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.

--
Peter

Nov 15 '05 #2
jeremy targett <jt@elsewhere.com> wrote:

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
Nov 15 '05 #3
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
Nov 15 '05 #4
Peter Nilsson <ai***@acay.com.au> wrote:
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
Nov 15 '05 #5
In article <dc**********@us23.unix.fas.harvard.edu>, jeremy targett wrote:
Hello, in my program I use 2-dimensional arrays to hold two integer values
for each of i members of a list. array[i][0] holds a starting
position, and array[i][1] holds a label - in fact I #defined START as 0
and LABEL as 1 so I can just write

array[i][START] = whatever
array[i][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[i][START] != S[i][START])
return (T[i][START]-S[i][START]);
for (i=0;i<len;i++)
if (T[i][LABEL] != S[i][LABEL])
return (T[i][LABEL]-S[i][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[i].start != S[i].start)
return (T[i][START]-S[i][START]);
for (i=0;i<len;i++)
if (T[i].label != S[i].label)
return (T[i].label-S[i].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.

--

Bye.
Jasen
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
14
by: ericellsworth | last post by:
Hi, I'm trying to use a class to pass variables back and forth from a form opened in dialog mode. I have created a class which invokes a form in its show method, like so: Public Sub Show() '...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
11
by: briankirkpatrick | last post by:
Forgive me if my post seems a little amateurish... I'm requesting assistance from some of you smart folks out there to get the managed calls write that meet the specification in the esa.h for...
16
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.