473,761 Members | 9,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cast multi-dimension array to pointer: T[][] to T*

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);
int a2[N][N];
int a3[N][N][N];

mtxrot1d((int*) a2);
mtxrot1d((int*) a3);

Thank you for your time.
Jun 27 '08 #1
7 3207
On Apr 26, 12:26 pm, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
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);
int a2[N][N];
int a3[N][N][N];

mtxrot1d((int*) a2);
mtxrot1d((int*) a3);

Thank you for your time.
Of course not.
It's a terrible lie to say that (int *) is equal to (int [N][N]). At
most, (int [N][N]) could be equal to (int (*)[N])
Jun 27 '08 #2
On Apr 26, 5:26*pm, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
* * mtxrot1d((int*) a2);
* * mtxrot1d((int*) a3);
sorry for the missed arguments:
mtxrot1d((int*) a2, i);
mtxrot1d((int*) a3, i);
Jun 27 '08 #3
On Apr 26, 5:34*pm, vipps...@gmail. com wrote:
On Apr 26, 12:26 pm, "lovecreatesbea ...@gmail.com"
* * int *mtxrot1d(int *p, int n);
* * int a2[N][N];
* * int a3[N][N][N];
* * mtxrot1d((int*) a2);
* * mtxrot1d((int*) a3);
It's a terrible lie to say that (int *) is equal to (int [N][N]). At
most, (int [N][N]) could be equal to (int (*)[N])- Hide quoted text -
K&R2 says multiple dimension array is kind of singly dimensional
array. For example these arrays: a1, and a2.

int a1[6] = {1, 2, 3, 4, 5, 6};
int a2[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *p;

+-+-+-+-+-+-+
a1: |1|2|3|4|5|6|
+-+-+-+-+-+-+

+-+-+-+ +-+-+-+
a2: |1|2|3| |4|5|6|
+-+-+-+ +-+-+-+

Their element type also are the same. We can do this address
arithmetic on singly dimension array a1:
p = (int*)a1;
p++;

Are we not guaranteed to do this address arithmetic on multiple
dimension array a2?
p = (int*)a2;
p++;
Jun 27 '08 #4
On Sat, 26 Apr 2008 02:26:34 -0700 (PDT),
"lovecreatesbea ...@gmail.com" <lo************ ***@gmail.comwr ote:
>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);
int a2[N][N];
int a3[N][N][N];

mtxrot1d((int*) a2);

Typos in the function calls corrected in OP's subsequent post.
mtxrot1d((int*) a3);
From a syntax point of view, yes. Ignoring the three exceptions you
should know by now, an expression with type "array of ..." is
converted to the address of the first element of the array with type
pointer to element type. Therefore
ptr = (cast)array;
is identical to
ptr = (cast)(&array[0]);
and you are allowed to cast an object pointer to a different type of
object pointer.

Whether the result of the operation is well defined is a different
question. However, in this case, the answer will always be yes.
Regardless of the number of dimensions in the array, the array name is
converted to the starting address of the array. This address is just
the address of the first byte occupied by the array. What the number
of dimensions does affect is the type of this value. Using your
examples, the expression a2 has type int(*)[N] and the expression a3
has type int(*)[N][N]. What the cast does is remove this "dimension
dependence" by converting both expressions to type int* without
changing the address itself.

In all cases, this is guaranteed to produce a pointer to the first
"basic element" of the array. This is because the array a3 and its
first element a3[0] start at the same address and this relationship is
recursive. So the array a3[0] and its first element a3[0][0] start at
the same address and the same for a3[0][0] and a3[0][0][0].
a3[0][0][0] is the fundamental element.

Please note that I have only addressed the initial value of the
conversion. Whether or not you can use the resulting pointer to step
through all the array elements created a heated discussion a few years
ago. Issues raised included bounds checking, fragmented memory
architectures, the DS6000 (not to be confused with the IBM disk
storage system named DS6800), language lawyering (regarding the
standard itself), etc. Given the current signal to noise ratio, I
hope this doesn't revitalize that topic.
Remove del for email
Jun 27 '08 #5
In article <17************ *************** *******@c19g200 0prf.googlegrou ps.com>
lovecreatesbea. ..@gmail.com <lo************ ***@gmail.comwr ote:
>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...
It is certainly syntactically valid. It is a somewhat dodgy thing
to do, though.

Given an actual array object, even if it is an array whose elements
are in turn arrays, you can always compute the "value" of the
(outermost, in this case) array. That is:

T arr[M][N];

arr;

computes the address of the first element of arr, i.e., &arr[0].
In this case, this value has type "T (*)[N]", i.e., "pointer to
array N of T". (If "arr;" is the entire statement, this computes
the value, then discards it.)

If we have an array of arrays of arrays:

T arr2[Z][M][N];

then &arr2[0] has type "pointer to array M of array N of T", or
"T (*)[M][N]".

It is even (I claim) "semantical ly valid", at least in a dodgy
fashion, since (I claim) the address of the first element of the
array-of-arrays is always "alignment compatible" with the address
of the first element of the "innermost" array. In particular, see
<http://web.torek.net/torek/c/pa.html>, and consider that the
alignment requirements of each circle (or ellipse) are "the same",
as they are solely determined by the "innermost" element type
(the type named "T" in the original poster's question).

However, the folks who interpret the various C Standards (C89 and
C99) tell us that using this "innermost element" pointer to access
the entire array contents is not guaranteed to work. I think C
would be a better language if it *were* guaranteed to work, and
in practice it seems to work on all existing implementations ,
but it may be wise to avoid depending on it, "just in case".

Even if it does always work, the "dodginess" is emphasized by the
need for careful "programmer-level work" to understand, maintain,
and correctly manage all the "stride" information (as it is called
in compiler circles) needed to access each array element. (Again,
see the web page above.) C99's Variable Length Arrays provide a
convenient "compiler-managed" method of working with the necessary
stride information, so that the programmer does not have to do all
the grunt work. If C made the guarantees that I wish it did, you
could do this (manually) in C89, instead of relying on the C99
compiler's VLAs, but it is probably better to use the C99 feature.
(Of course, this assumes your C compiler supports it.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jun 27 '08 #6
"lovecreatesbea ...@gmail.com" <lo************ ***@gmail.comwr ites:
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);
int a2[N][N];
int a3[N][N][N];

mtxrot1d((int*) a2);
mtxrot1d((int*) a3);

Thank you for your time.
Others have discussed the potential dangers of doing the conversion.
I'll just mention that you don't need to use a cast to do it.

Conversion from T[] to T* happens automaticalliy in most contexts, so given

int a1[N];

you can just write:

mtxrot1d(a1);

If you have, say, a 3-dimensional array of T, you can obtain
the first T object in the array via indexing:

a3[0][0][0]

and its address with an "&" operator:

&a3[0][0][0]

The latter could be "simplified " to this:

a3[0][0]

assuming it's used in a context that triggers the usual
array-to-pointer conversion ("decay"), but I find &a3[0][0][0]
to be clearer.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #7
lovecreatesbea. ..@gmail.com wrote:
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);
int a2[N][N];
int a3[N][N][N];

mtxrot1d((int*) a2);
mtxrot1d((int*) a3);
If you want &a3[0][0][0], then you need only write that.

Apart from the direct conversion from int (*)[][] to int *
being technically undefined, if we take the most obvious
result to be one of 'repeated' decay, i.e. &a[0]...[0],
then you still face the problem that _that_ pointer can
only access the elements of the inner most array
(in the above samples, N elements).

--
Peter
Jun 27 '08 #8

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

Similar topics

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 ;
2
1379
by: Robert | last post by:
Lets say you have an multi array of ints num 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)). Why cant I use X(int **num) ? //Robert
3
1549
by: Makiyo | last post by:
how do u do something like this char x; x = "hello"; I got an error, but is there a way I can do it without using pointer? thx ; )
12
4753
by: natkw1 | last post by:
Hi, I'm attempting to understand the use of pointers(at least grasp how pointers work). I've read the FAQ on http://www.eskimo.com/~scs/C-faq/s6.html on pointers and arrays but I'm still a bit lost. I written the following code to try to understand it but it's not working:
0
1103
by: Hai Ly Hoang | last post by:
Hi, I'm using VC++ 7.0 (VS 2003). My code: void main() { int *a = new int; } I'm sure that the above code is accepted by VC++ (no compiling error). What is exactly the semantic of the above code ? Is is an implicit conversion from multi-dimension array to 1-dimension array ? If yes, what is it usage ?
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
1873
by: Paul | last post by:
Hi, This is probably a no-brainer, but I could do with some help here. I have some code which looks like this $event = array("Date"=>array(), "Time"=>array(), "Venue"=>array(), "Event"=>array()); $line = array();
5
2119
by: alexandis | last post by:
I have the following multi array like this: = ="A", ="B", ="C" ] = ="A", ="C", ="D" ] = ="B", ="A", ="B" ] = ="B", ="A", ="D" ] It should be transformed into this: = =, = ],
17
2395
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
0
9376
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
9811
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...
0
8813
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
7358
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
6640
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
5266
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
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
2788
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.