473,770 Members | 5,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multi dimension arrays

Lets say you have an multi array of ints num[x][y] and send
it as a pointer to a function X(arg) have do a do this without
define the size?

It works with X(int (*num)[0]).
Why cant I use X(int **num) ?

//Robert
Nov 14 '05 #1
2 1379
"Robert" <ol**********@h otmail.com> wrote in message
news:pa******** *************** *****@hotmail.c om...
Lets say you have an multi array of ints num[x][y] and send
it as a pointer to a function X(arg) have do a do this without
define the size?

It works with X(int (*num)[0]).
Why cant I use X(int **num) ?


This is addressed in the FAQ:

http://www.eskimo.com/~scs/C-faq/q6.18.html

You might also want to look at questions 6.19 and 6.20.

--
Russell Hanneken
rg********@pobo x.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #2
"Robert" <ol**********@h otmail.com> wrote in message
news:pa******** *************** *****@hotmail.c om...
Lets say you have an multi array of ints num[x][y] and send
it as a pointer to a function X(arg) have do a do this without
define the size?
I find it difficult to parse your question. What language is it in?
It works with X(int (*num)[0]).
Why cant I use X(int **num) ?


Define "works". In function parameter context, the two declarations above
are equivalent. Unfortunately, they are both (most probably) wrong.
Consider:

int X (int (*num)[0]);

int main (void)
{
int x[3][4];
X(x);
return 0;
}

Your 'x' is an array of three arrays of four ints, organized in memory like
this:

x[0] x[1] x[2]
+-----------------+-----------------+-----------------+
| [0] [1] [2] [3] | [0] [1] [2] [3] | [0] [1] [2] [3] |
+-----------------+-----------------+-----------------+
^ ^ ^
x[0][0] x[1][0] x[1][3] ...etc.

To get from x[0][0] to x[1][0], you need to skip exactly four ints, i.e.
sizeof x[0] (or 4*sizeof(int) if you like) bytes.

But your function X expects an array of pointers to int. Array of
unspecified size of pointers to int, to be more precise. Like this:

x[0] x[1] x[2] ...
+----+----+----+--
| *0 | *1 | *2 | ...
+----+----+----+--

To get to x[1][0], for example, you need to skip sizeof x[0] bytes as in the
first example, take the pointer from there, add an index (0 in your case)
and dereference it.

I /can/ imagine a platform where sizeof x[0] is the same in both cases, i.e.
sizeof(int*) == 4*sizeof(int), but believe me, such platforms are rare.

So, int[x][y] and int(*)[0] are not equivalent types. There was a recent
post by Chris Torek on this topic, explaining exactly how the former could
decay to the latter in a function call, but that does not make it right.
(Unfortunately I don't recall the subject of that thread, sorry.)

The correct way to declare multi-dimensional arrays in function arguments
is:

int fun (int array[][X][Y][Z]);

Add as many dimensions as you like (up to six, <g>), but only the first one
can be unspecified. Your X, Y, Z etc are the same constants that you used
for declaring the array that you pass to the function.

I hope this make sense.

Peter
Nov 14 '05 #3

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

Similar topics

11
2092
by: Alan Searle | last post by:
I have been trying to get an array to sort on a particular column and have been having problems with my main key ... array_multisort($dirlist, SORT_DESC, SORT_STRING); This function takes an array ($dirlist) which has been loaded with a directory listing where each directory has its own index number ("dirid") and displays the listing in descending order by name ("dirname"). This works fine for the sorting of the names but the index...
5
2555
by: Cant Think Today | last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g 1,2,3,4,5 1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6 I think a bit of code that will iterate over these arrays to print out the element indices for each unique element in the N-dimensional array. E.g. for the above
6
2234
by: Adam Hartshorne | last post by:
The input to a function of a 3rd party library I want to use requires a double**, which is a multi-dimension array of doubles. I have looked on the net etc and seen several ways of supposedly doing this, but I don't seem to be able to get them to work. I was wondering if anybody can tell me what I am doing wrong. int rows = 10 ; int cols = 10 ;
8
2252
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along that a declaration like: int intArray means that there is an array of pointers to int with a size of 5, each of whose elements is an array of int with a size of 3. This definition seemed intuitive to me since a declaration like
11
4469
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
1
2571
by: SouthSpawn | last post by:
If I have an array that is string MyArray = new string Then I do the following. string = "A"; How can I "ReDimension" my multi dimension array? In VB.Net they have the ReDim Keyword.
4
5893
by: rmorvay | last post by:
I have a requirement to search a multi-dimensional array for an item, then delete the item and "reset" the array so that their are no gaps in the resulting array. I have been trying to figure out how to use the Array.Copy method to accomplish this but it seems to only work for single dimension arrays. Here is a code sample: 'dim arrTest(600,3) as string 'Actual array dimension needed dim arrTest(600) as string 'Used only in this test...
7
3896
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to allocate multidimentional arrays like so: int *v_base = (int *) malloc(1000000*sizeof(int)); int **v = (int **) malloc(1000*sizeof(int *));
3
2126
by: ben.r.wood | last post by:
I am not entirely sure, but after scanning the web believe I need to use multi-dimensional arrays for my problem - although I have not seen any examples of what I am trying to achieve. I have a form with one field, for which a user will submit a lump of delimited data. The data will always be in the same format, ie. 5 fields per record, each field has a delimeter and a new row starts a new record. for example:
7
3208
by: lovecreatesbea... | last post by:
Is it always legal to cast expressions of type of multi-dimension array to type of pointer? Including: T to T* , T to T* , T to T* , and so on... For example: int *mtxrot1d(int *p, int n);
0
9425
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
10225
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...
1
10001
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
9867
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
7415
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
6676
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
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.