473,414 Members | 1,675 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,414 software developers and data experts.

Two-dimensional array to array of arrays

Given the following current definition:

int array[100][2];

Is the following equivalent?

(By "equivalent", I mean that any reference to "array[x][y]" retains
the same meaning, and that the memory layout of the 200 ints is
identical.)

typedef int TwoInts[2];
TwoInts array[100];

What about:

TwoInts *array = malloc( 100 * sizeof(*array) );

Yes, I realize that type actual type of "array" has changed, and that
malloc() may fail, and so on.

I just need to verify that "array[x][y]" hasn't changed meaning. A
quick test program seems to confirm that the behavior has not changed,
but I know that implementation-defined behavior may "work" on one
platform an not another. I'm pretty sure that the behavior here is
defined by the standard, and not implementation-defined, but I'd
like confirmation.

Thanks.

==========
#include <stdio.h>

typedef int TwoInts[2];
int array[10][2];

int main(int argc,char *argv[])
{
int i,j;
TwoInts *x;

for ( i=0 ; i < 10 ; i++ )
for ( j=0 ; j < 2 ; j++ )
array[i][j] = i*10 + j;

for ( i=0 ; i < 10 ; i++ )
{
for ( j=0 ; j < 2 ; j++ )
printf("[%d][%d] = %2d ",i,j,array[i][j]);
printf("\n");
}

printf("\n");
x = array;

for ( i=0 ; i < 10 ; i++, x++ )
{
for ( j=0 ; j < 2 ; j++ )
printf("[%d][%d] = %2d ",i,j,(*x)[j]);
printf("\n");
}

return(0);
}
==========

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Dec 21 '06 #1
2 1712

Kenneth Brody wrote:
Given the following current definition:

int array[100][2];
array is an array of 100 items, each of which is an array of 2 ints.
Is the following equivalent?
typedef int TwoInts[2];
TwoInts array[100];
array is an array of 100 items, each of which is an array of 2 ints.
>
What about:

TwoInts *array = malloc( 100 * sizeof(*array) );
array is a pointer to 100 items, each of which is an array of 2 ints.
It can be handled as an array.

That's my interpretation.

Dec 21 '06 #2
Kenneth Brody wrote:
Given the following current definition:

int array[100][2];
array has type int[100][2], which decays to an int(*)[2] pointing at the
first two-int element of an array of 100 such elements.
Is the following equivalent?

(By "equivalent", I mean that any reference to "array[x][y]" retains
the same meaning, and that the memory layout of the 200 ints is
identical.)

typedef int TwoInts[2];
TwoInts array[100];
array has the same type int[100][2], which of course decays in the same way.
What about:

TwoInts *array = malloc( 100 * sizeof(*array) );
array is an int(*)[2] and, assuming malloc succeeded, points at the
first two-int element of an array of 100 such elements.

They decay to the same thing, when used in any other way apart from as
the operand of unary & or sizeof.

It may be used identically to the first two, except as an operand to
unary & or sizeof.

In the first two cases case &array is int(*)[100][2]
In the last case &array is int(**)[2]

In the first two cases sizeof array is 100*2*sizeof(int)
In the last case, sizeof array is sizeof(int(*)[2]) which is usually the
same size as any other object pointer: 2, 4 or 8 bytes are typical.

--
Simon.
Dec 21 '06 #3

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

Similar topics

0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
8
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). ...
6
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
10
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a...
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
9
by: dhable | last post by:
I just started working with Python and ran into an annoyance. Is there a way to avoid having to use the "from xxx import yyy" syntax from files in the same directory? I'm sure it's been asked a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
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...

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.