473,888 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another Question

I'm working on my homework for C++, and have a problem. I have a
multi-dimension array, Array1[5][4] that I can't define in the function
declaration and function definition. I keep getting an error. I've tried
the following:

void Sum ( int [][] );

I get an error with this, and also with:

void Sum ( int [] );

The compiler, Microsoft VC6 can't convert the two-dimensional array to a
one-dimension array.

What I'm I doing wrong? I can't find the answer in my notes or the book,
they all deal with one-dimension arrays.

John Howard
Jul 19 '05 #1
3 3151
John Howard wrote:
I'm working on my homework for C++, and have a problem. I have a
multi-dimension array, Array1[5][4] that I can't define in the function
declaration and function definition. I keep getting an error. I've tried
the following:

void Sum ( int [][] );

I get an error with this, and also with:

void Sum ( int [] );

The compiler, Microsoft VC6 can't convert the two-dimensional array to a
one-dimension array.

What I'm I doing wrong? I can't find the answer in my notes or the book,
they all deal with one-dimension arrays.


The array declaration

int a[3][2]

declares an array variable 'a' that has 3 elements, where each element
'a[n]' is an object of type 'int[2]':

typeof(a[n]) == int[2]

IOW, each element in 'a' is an "array object" consisting of 2 int
subobjects:

a[0] ::= [[int][int]] // int[2] object
a[1] ::= [[int][int]] // int[2] object
a[2] ::= [[int][int]] // int[2] object

Recall that when you pass an array to (or return an array from) a
function, what you are really doing is passing the address of the
array's first element:

void g1 (int *q, int size);
void g2 (int q[], int size); // alternate "array argument" syntax

int main() {
int b[5];

g1(b, 5); // i.e., g1(&b[0], 5);
g2(b, 5); // i.e., g2(&b[0], 5);

/* n.b.
* typeof(b[0]) == int
* typeof(&b[0]) == int*
*/
}

Since 'a[0]' is an object of type 'int[2]', the address of a's first
element (i.e., '&a[0]') will be a pointer whose type is 'int(*)[2]':

typeof(a[0]) == int[2]
typeof(&a[0]) == int(*)[2]

So to pass the address of array a's first element (i.e., &a[0]) into a
function, you need to declare the function's arguments accordingly:

<example>
// n.b. In functions f1 and f2 below, the formal parameter 'p'
// is a pointer to an 'int[2]' object.

void f1 (int(*p)[2], int rows)
{
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < 2; ++c) {
p[r][c] = 0;
}
}
}

// or, using the alternate "array argument" syntax,

void f2 (int p[][2], int rows) { /* ... */ }

int main()
{
int a[3][2];
f1 (a, 3);
f2 (a, 3);
}
</example>

n.b. Applying the array indexing operator '[]' to an int[2] object lets
you access the individual int subobjects within the int[2] object:

typeof(p[r]) == int[2]
typeof(p[r][c]) == int

e.g.

p[r] ::= [[567][222]]
p[r][0] ::= 567
p[r][1] ::= 222

FWIW, these concepts carry over to all N-dimension arrays:

void d1 (int (*parray)[4][3], int dim1);
void d2 (int parray[][4][3], int dim1); // alternate syntax

int c[5][4][3];

d1 (c, 5); // i.e., d1(&c[0], 5);
d2 (c, 5); // i.e., d2(&c[0], 5);

typeof(c) == int[5][4][3]
typeof(&c) == int(*)[5][4][3]

typeof(c[i]) == int[4][3]
typeof(&c[i]) == int(*)[4][3]

typeof(c[i][j]) == int[3]
typeof(&c[i][j]) == int(*)[3]

typeof(c[i][j][k]) == int
typeof(&c[i][j][k]) == int(*) == int*

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link58 09{at}now.here. com
Jul 19 '05 #2
hi,
i would do it quick and dirty, if everything else fails:
void Sum(void* lpBlabla)// prototype
cast it back to int[][] in function Sum

call it so:
Sum((void*)&Sum[0][0]);


-------- Original Message --------
Subject: Another Question (18-Jul-2003 17:45)
From: John Howard <jd********@yah oo.com>
To: comp.lang.c++
I'm working on my homework for C++, and have a problem. I have a
multi-dimension array, Array1[5][4] that I can't define in the function
declaration and function definition. I keep getting an error. I've tried
the following:

void Sum ( int [][] );

I get an error with this, and also with:

void Sum ( int [] );

The compiler, Microsoft VC6 can't convert the two-dimensional array to a
one-dimension array.

What I'm I doing wrong? I can't find the answer in my notes or the book,
they all deal with one-dimension arrays.

John Howard


Jul 19 '05 #3


Administratoren wrote:

hi,
i would do it quick and dirty, if everything else fails:
void Sum(void* lpBlabla)// prototype
cast it back to int[][] in function Sum

call it so:
Sum((void*)&Sum[0][0]);


That's the worst advise one could give.
Please refrain from giving such advises in
the future.

Thank you.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #4

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

Similar topics

3
2309
by: fig000 | last post by:
Hi, I'm relatively new to Javascript so please bear with me on what might sound like silly questions. This is what I want to do: I'm working in classic asp (I have to for this project). I need to: 1. Click a submit button on an asp page, calling another page that saves the contents of that form to a database and then displays
6
1848
by: anon | last post by:
Post Forwarding question...... For this control below, <asp:Button runat="server" PostTargetUrl="page2.aspx" /> The Attribute: PostTargetUrl="page2.aspx" Is this PostTargetUrl Attribute going to be available in the <a> and Html Controls as well as opposed to just the <asp:Button> control?
2
2681
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is less normalised, but seems less computationally expensive to me(?). Basicially what I have is: Friend (uid1,uid2,fid) -> FriendshipCategories(fid,cid) -> Categories(cid,name) If I want to find out all the 'categories' (names) which a user...
0
1439
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl (classname:QuestionControl) as many times as there are rows in a DataTable (also named Questions) in a DataSet. But this UserControl is ,for reasons of structuring, not a member of the WebForm Object in which it should be displayed, it is member of another class...
3
1410
by: Brad | last post by:
I have another hopefully simple question. I am so used to writing VB .Net windows apps that there are some things in ASP .Net that just does not easily cross over. I know how to pass variables to another form, but how would I do this from one page to another? I am not finding a simple solution. Thanks for the help
27
2571
by: Javier Martinez | last post by:
Hi I have asp application in a machine with a virtual directory referring a shared directory in another machine When I try to load any aspx page of my portal I get the following error: Mensaje de error del analizador: We can't load the type 'JULIAN.Global'.
17
2736
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not pattern1 print >>orcfilename, line I am pretty sure my code isn't close to what I want. I need to be able
24
3003
by: David Mathog | last post by:
On a Solaris 8 system if a user "joe" logs in, for instance via ssh, cuserid() returns "joe". That's the expected behavior and so far so good. However if that user then does: % su - sally cuserid will still return "joe". However "sally" uses "tcsh" where whoami shows "sally". If the user running as "sally" creates a file the ownership is for "sally". "ps -ef" also shows the user shell running as "sally". While "sally"
13
2468
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever works best) to display a dropdown menu of fields populated from table tblInvoiceData. This control also includes a textbox which the user can input a value. These two columns are side by side and not in a vertical layout. The user then clicks on...
4
4359
by: MichaelK | last post by:
Hello. I have all data already collected on the current page? I want to open another window with the form, fill the fields and submit that form. So basically the question is how can I fill all fields and submit the form on another window. Regards, Michael
0
9961
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
11178
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
10777
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
10882
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
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7990
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
7148
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();...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3251
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.