473,808 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1635
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.c om> 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**********@u s23.unix.fas.ha rvard.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(thingt ype 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,size of(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
2968
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 p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the advantage/disadvantage of either of
14
3015
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() ' This method shows the form used to get the info If sWhereInt = "" Then DoCmd.OpenForm sFormNameInt, acNormal, , , acFormAdd, _
1
2915
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 written in C#. When the app calls my unmanaged functions, they work fine. But as soon as my unmanaged functions call managed functions (in the same source file!), the app reports an "unknown exception" error.
1
2605
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, System::EventArgs * e) { MyDLLInit(MyAppDisplayFunction); }
5
3447
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
2
3153
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 the stack pointer and it does it by the command "add esp,8" after returning from the called function. My questions: 1. Is the stack pointer common in a certain thread(or process)? 2. How does the called function get the parameters, is it by...
18
4365
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
15
22872
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 could either be f1() or f2(). BRs!
11
3203
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 Esa_Init. When I make a call, VS2005 reports "AccessViolationException" and refers to the 4th parameter (EsaT_State_Handle). I don't know how to define nor pass this reference correctly to the legacy DLL. What am I doing wrong? Thanks in...
16
517
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 and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10631
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.