473,396 Members | 2,009 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,396 software developers and data experts.

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 3189
On Apr 26, 12:26 pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
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.comwrote:
* * 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.comwrote:
>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**********************************@c19g2000prf. googlegroups.com>
lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
>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) "semantically 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.comwrites:
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_Keith) <ks***@mib.org>
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
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...
2
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) ?...
3
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
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...
0
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...
1
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
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(),...
5
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
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) {
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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,...
0
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...
0
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...
0
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...
0
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,...

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.