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

layout of arrays

AC
I noticed that if I declared an array a as
int a[2][3][1], sizeof a is 6 * sizeof(int).

This means that a occupies 6*sizeof(int) consecutive bytes, right? So I can
consider, (int *) &a as a pointer to an array of 2*3*1=6 ints. Is this
correct?

This code works fine for me :

#include <stdio.h>

int
main(void)
{
int a[2][3][1]={1,2,3,4,5,6};
int *p;
int i;
printf("sizeof a =%u\n",(unsigned int) sizeof a);
p = (int *) &a;
for(i=0; i < sizeof a/sizeof(int);i++)
printf("p[%d]=%d\n",i,p[i]);
return 0;
}
Nov 14 '05 #1
3 1380

"AC" <te**@test.test> wrote
I noticed that if I declared an array a as
int a[2][3][1], sizeof a is 6 * sizeof(int).

This means that a occupies 6*sizeof(int) consecutive bytes, right? So I
can consider, (int *) &a as a pointer to an array of 2*3*1=6 ints. Is
this correct?

Yes, but be warned that everything gets horrible as soon as you try to use
such arrays in non-trivial ways.
Generally if you need a 3d array, consider whether the dimensions are really
fixed at compile time. The consider whether it makes sense to write your
subroutines so that they can only operate on an array of fixed dimensions.
If the answers to these questions are no, you will save yourself a lot of
grief by building the array with calls to malloc().
Nov 14 '05 #2
AC wrote:

I noticed that if I declared an array a as
int a[2][3][1], sizeof a is 6 * sizeof(int).

This means that a occupies 6*sizeof(int) consecutive bytes, right?
So I can consider, (int *) &a as a pointer to an array of 2*3*1=6
ints. Is this correct?

This code works fine for me :

#include <stdio.h>

int
main(void)
{
int a[2][3][1]={1,2,3,4,5,6};
int *p;
int i;
printf("sizeof a =%u\n",(unsigned int) sizeof a);
p = (int *) &a;
for(i=0; i < sizeof a/sizeof(int);i++)
printf("p[%d]=%d\n",i,p[i]);
return 0;
}


Looks fine to me. You might want to try this modification, playing
with the various *DIM defines, to see the correspondence of the
linear array and the multidimensional array. C only has a single
array dimension, and multidimensional arrays simply are a mapping
into a single dimensional array. Notice How I have intermixed
array and pointer notation.

Keep the dimensions below 10 so the displays show all coordinates.

#include <stdio.h>

#define XDIM 4
#define YDIM 3
#define ZDIM 2

#define LINEMAX 72

void dump(int a[][YDIM][ZDIM], size_t sz)
{
int linesize;
size_t ix;
int *p;

p = (int *)a;
linesize = 0;
for (linesize = 0, ix = 0; ix < sz; ix++) {
linesize += printf("p[%2d]=%0.3d", (int)ix, p[ix]);
if (linesize <= LINEMAX) {
putchar(' '); linesize++;
}
else {
putchar('\n'); linesize = 0;
}
}
if (linesize) putchar('\n');
} /* dump */

/* -------------------- */

void inita(int a[][YDIM][ZDIM])
{
int x, y, z;
size_t ix;
int *p;

p = (int *)&a;
for (x = 0; x < XDIM; x++)
for (y = 0; y < YDIM; y++)
for (z = 0; z < ZDIM; z++) {
ix = (((z * ZDIM) + y) * YDIM) + x;
a[x][y][z] = ((z * 10 + y) * 10) + x;
}
} /* inita */

/* -------------------- */

int main(void)
{
int a[XDIM][YDIM][ZDIM] = {1,2,3,4,5,6};
int *p;
size_t ix;

printf("sizeof a = %lu\n", (unsigned long) sizeof a);
p = (int *) &a;
for (ix = 0; ix < sizeof a/sizeof(int); ix++)
printf("p[%d]=%d\n", (int)ix, p[ix]);
dump(a, sizeof a/sizeof(int));
inita(a);
dump(a, sizeof a/sizeof(int));
return 0;
}
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #3
On Wed, 18 May 2005 11:08:40 -0400, "AC" <te**@test.test> wrote:
I noticed that if I declared an array a as
int a[2][3][1], sizeof a is 6 * sizeof(int).

This means that a occupies 6*sizeof(int) consecutive bytes, right? So I can
consider, (int *) &a as a pointer to an array of 2*3*1=6 ints. Is this
correct?

In practice but not quite 100% formally. It is clearly required by the
standard that the storage (representation) of an array be the elements
in order without additional padding, and for multidim arrays applying
this rule repeatedly requires row-major order. ("additional" because
there can be padding _within_ a struct (or union) element, including
at its end for alignment.)

It is not clearly required that you can _access_ the "flattened"
elements by a non-character pointer so ( (int*)&a ) [4] isn't itself
guaranteed. But it is required that a character pointer step through
the entire representation, so * (int*) ( (char*)&a + 4*sizeof(int) )
must. In practice, there's no reasonable way to implement the latter
without implementing the former, not to mention that it just "makes
sense" anyway, and everyone does. I think I recall a couple of
committee members commenting on comp.std.c that they intended this,
but couldn't find words they were certain adequately specified C's
historically loose memory model without risking possible problems in
other areas and let the sleeping dog lie. Especially since enough
programs and programmers do this that any implementor who broke it,
even if they could argue that they weren't violating the standard,
would be shunned by most if not all users.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #4

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

Similar topics

0
by: Peter Hitchmough | last post by:
What is the *truth* when it comes to configuring Oracle files on an Enterprise Virtual Array like an HP EVA3000 or EVA5000 or an EMC Symmetrix frame. As I Understand It - A Virtual Array is a...
82
by: Peter Diedrich | last post by:
The site design is pretty simple: ============================================ | Head | ============================================ | | ...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
3
by: Josema | last post by:
Hi, I have a web application, that takes from excel a range of rows and columns... The method that gets the data in the ranges returns an Array of two dimensions with the data... Then i...
1
by: Fuzzy via .NET 247 | last post by:
I have a struct that I would like to control the field layout on in the same manner of UNION structs in C++. This is not for passing data to unmanaged code, it is to save memory space because field...
0
by: Emil Briggs | last post by:
I am working with an application whose performance is limited by the write activity (inserts, updates and deletes) on three specific tables. The database is RAM resident so read performance is not...
10
by: Luke | last post by:
Hi. I am trying to make correct layout, here is an example of (dynamically generated content via jsp): http://localhost/www/layout.htm Most outer div is positioned absolute (if not then it...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
1
by: Adrian Hawryluk | last post by:
I was wondering, does this meet the criteria for "layout compatible" in the C++ specification? struct A { int a; }; struct B { A b; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...

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.