473,699 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",(unsigne d 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 1395

"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",(unsigne d 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 multidimensiona l array. C only has a single
array dimension, and multidimensiona l 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.c om, 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.ne t
Nov 14 '05 #4

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

Similar topics

0
2379
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 storage appliance with a large number of disks (say over 50), dual-path controller access and, significantly, a very large disk cache. The cache is supported by front-end and back-end (disk-side) processors and a range of algorithms that aim to...
82
10680
by: Peter Diedrich | last post by:
The site design is pretty simple: ============================================ | Head | ============================================ | | | | | | | | | left | center | right | | | | | | | | |
47
9140
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
1489
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 render the data in the browser for instance using html tables.... My problem its that:
1
2415
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 X will either be short integer or double, and other info in the struct can be used to determine if X should be interpreted as short integer or double. I have found the docs for and for . However, one doc state that these fields only control...
0
2587
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 limited by the disk speeds. We are using Postgres 8 and I am wondering about the best way to set up tablespaces. We have 3 disk arrays. Would it make more sense to place each of tables 1,2 and 3 and their associated indexes on their own...
10
9200
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 will not grow when content inside div.body is greater than width of window of user agent), anyway anyone knowlegable can see it in sources...
14
4849
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 can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
1
1311
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
8620
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
9180
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...
0
9038
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8887
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
6536
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
5877
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
4378
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
2351
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2012
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.