473,545 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multi-dimension array assignments

Aloha!

I've been reading the standard (May '05 draft, actually) and stumbled across
this:
6.7.1 Initialization
§20 "If the aggregate or union contains elements or members that are
aggregates or unions, these rules apply recursively to the subaggregates or
contained unions. If the initializer of a subaggregate or contained union
begins with a left brace, the initializers enclosed by that brace and its
matching right brace initialize the elements or members of the subaggregate
or the contained union. Otherwise, only enough initializers from the list
are taken to account for the elements or members of the subaggregate or the
?rst member of the contained union; any remaining initializers are left to
initialize the next element or member of the aggregate of which the current
subaggregate or contained union is a part."

Fine, so this mean it is actually OK to init a multi-dim array as
short a[2][3] = {0,1,2,3,4,5};
§26 Example 3 actually confirms that.

Just in case someone would wonder, line 10 is valid according to section
6.5.2.5§6 regarding "anonymous" compound literals.

So I set up some test to try to learn more about these:
/* 1 */ #include <stdio.h>
/* 2 */ #include <string.h>
/* 3 */
/* 4 */ int main()
/* 5 */ {
/* 6 */ short a[2][3] = {0,1,2,3,4,5};
/* 7 */ short b[6];
/* 8 */ short c[6];
/* 9 */ memcpy(b, a, sizeof b);
/*10 */ memcpy(c, (int[2][3]){{0,1,2},{3,4, 5}}, sizeof c);
/*11 */ if (!memcmp(a, b, sizeof b)) printf("a = b\n");
/*12 */ if (!memcmp(a, c, sizeof c)) printf("a = c\n");
/*13 */ if (!memcmp(b, c, sizeof c)) printf("b = c\n");
/*14 */ return 0;
/*15 */ }

Compiled with "gcc -std=c99 -pedantic -Wall file.c", it however complains
that:
file.c: In function `main':
file.c:6: warning: missing braces around initializer
file.c:6: warning: (near initialization for `a[0]')

Running the program, I end up with all 3 arrays having the same values:
a = b
a = c
b = c

Here are a couple of questions:
1) About the compiler warning, either 1) this is not in the official C99
standard and is one of the proposed feature by the commitee or 2) gcc is
wrongly complaining about esthetics (like it suggests double parentheses in
assignations in conditionals), in which case this is off-topic here. Which
one is it?

2) Are lines 9 and 10 assignments logically equivalent (except of course
that b != c)? My guess is "yes" as both are copying from a short[2][3]
memory location. Given that line 6 construct is o.k by 6.7.1§20, this
should mean that line 13 will always compare to 1, whether or not what I
actually meant to be copied is what I got (see #3).

3) Will b and/or c contain the array {0,1,2,3,4,5} after lines 9 and 10? My
guess is "yes" as array members need to be contiguous in memory, so arrays
of arrays members (ie.: 1-dim arrays) would also be contiguous in memory as
they are arrays too. However, I'm not sure if there can be padding between
(n-1)dimensional-array elements in (n)dimensional arrays, where n>1, in
which case I would be wrong in my assumption.

Thanks,
--
Eric Laberge
Nov 15 '05 #1
3 2533
Eric Laberge wrote:
Aloha!

I've been reading the standard (May '05 draft, actually) and stumbled across
this:
6.7.1 Initialization
§20 "If the aggregate or union contains elements or members that are
aggregates or unions, these rules apply recursively to the subaggregates or
contained unions. If the initializer of a subaggregate or contained union
begins with a left brace, the initializers enclosed by that brace and its
matching right brace initialize the elements or members of the subaggregate
or the contained union. Otherwise, only enough initializers from the list
are taken to account for the elements or members of the subaggregate or the
?rst member of the contained union; any remaining initializers are left to
initialize the next element or member of the aggregate of which the current
subaggregate or contained union is a part."

Fine, so this mean it is actually OK to init a multi-dim array as
short a[2][3] = {0,1,2,3,4,5};
§26 Example 3 actually confirms that.

Just in case someone would wonder, line 10 is valid according to section
6.5.2.5§6 regarding "anonymous" compound literals.

So I set up some test to try to learn more about these:
/* 1 */ #include <stdio.h>
/* 2 */ #include <string.h>
/* 3 */
/* 4 */ int main()
/* 5 */ {
/* 6 */ short a[2][3] = {0,1,2,3,4,5};
/* 7 */ short b[6];
/* 8 */ short c[6];
/* 9 */ memcpy(b, a, sizeof b);
/*10 */ memcpy(c, (int[2][3]){{0,1,2},{3,4, 5}}, sizeof c);
/*11 */ if (!memcmp(a, b, sizeof b)) printf("a = b\n");
/*12 */ if (!memcmp(a, c, sizeof c)) printf("a = c\n");
/*13 */ if (!memcmp(b, c, sizeof c)) printf("b = c\n");
/*14 */ return 0;
/*15 */ }

Compiled with "gcc -std=c99 -pedantic -Wall file.c", it however complains
that:
file.c: In function `main':
file.c:6: warning: missing braces around initializer
file.c:6: warning: (near initialization for `a[0]')

Running the program, I end up with all 3 arrays having the same values:
a = b
a = c
b = c

Here are a couple of questions:
1) About the compiler warning, either 1) this is not in the official C99
standard and is one of the proposed feature by the commitee or 2) gcc is
wrongly complaining about esthetics (like it suggests double parentheses in
assignations in conditionals), in which case this is off-topic here. Which
one is it?
A compiler may issue a warning about anything it likes. Literally.
There are many completely legal constructs that many, if not most or
all, compilers will warn about -- including, for example, using an
assignment expression as the conditional in an `if' or `while'
statement. Effectively it's asking, "Are you sure?"
2) Are lines 9 and 10 assignments logically equivalent (except of course
that b != c)? My guess is "yes" as both are copying from a short[2][3]
memory location. Given that line 6 construct is o.k by 6.7.1§20, this
should mean that line 13 will always compare to 1, whether or not what I
actually meant to be copied is what I got (see #3).
Yes.
3) Will b and/or c contain the array {0,1,2,3,4,5} after lines 9 and 10? My
guess is "yes" as array members need to be contiguous in memory, so arrays
of arrays members (ie.: 1-dim arrays) would also be contiguous in memory as
they are arrays too. However, I'm not sure if there can be padding between
(n-1)dimensional-array elements in (n)dimensional arrays, where n>1, in
which case I would be wrong in my assumption.

There cannot. You are right.

HTH and Cheers,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Nov 15 '05 #2
Eric Laberge wrote:
Aloha!

I've been reading the standard (May '05 draft, actually) and stumbled across
this:
6.7.1 Initialization
That's actually 6.7.8.
§20 "If the aggregate or union contains elements or members that are
aggregates or unions, these rules apply recursively to the subaggregates or
contained unions. If the initializer of a subaggregate or contained union
begins with a left brace, the initializers enclosed by that brace and its
matching right brace initialize the elements or members of the subaggregate
or the contained union. Otherwise, only enough initializers from the list
are taken to account for the elements or members of the subaggregate or the
?rst member of the contained union; any remaining initializers are left to
initialize the next element or member of the aggregate of which the current
subaggregate or contained union is a part."

Fine, so this mean it is actually OK to init a multi-dim array as
short a[2][3] = {0,1,2,3,4,5};
§26 Example 3 actually confirms that.

Just in case someone would wonder, line 10 is valid according to section
6.5.2.5§6 regarding "anonymous" compound literals.

So I set up some test to try to learn more about these:
/* 1 */ #include <stdio.h>
/* 2 */ #include <string.h>
/* 3 */
/* 4 */ int main()
Should be "int main (void)"
/* 5 */ {
/* 6 */ short a[2][3] = {0,1,2,3,4,5};
/* 7 */ short b[6];
/* 8 */ short c[6];
/* 9 */ memcpy(b, a, sizeof b);
/*10 */ memcpy(c, (int[2][3]){{0,1,2},{3,4, 5}}, sizeof c);
/*11 */ if (!memcmp(a, b, sizeof b)) printf("a = b\n");
/*12 */ if (!memcmp(a, c, sizeof c)) printf("a = c\n");
/*13 */ if (!memcmp(b, c, sizeof c)) printf("b = c\n");
/*14 */ return 0;
/*15 */ }

Compiled with "gcc -std=c99 -pedantic -Wall file.c", it however complains
that:
file.c: In function `main':
file.c:6: warning: missing braces around initializer
file.c:6: warning: (near initialization for `a[0]')

Running the program, I end up with all 3 arrays having the same values:
a = b
a = c
b = c

Here are a couple of questions:
1) About the compiler warning, either 1) this is not in the official C99
standard and is one of the proposed feature by the commitee or 2) gcc is
wrongly complaining about esthetics (like it suggests double parentheses in
assignations in conditionals), in which case this is off-topic here. Which
one is it?
Neither. The official C99 document contains the verbiage you provided
above, the code is conforming. Compilers are allowed to complain about
whatever they like so long as the compile a conforming program. The
authors of gcc apparently feel that a warning is warranted here.
2) Are lines 9 and 10 assignments logically equivalent (except of course
that b != c)? My guess is "yes" as both are copying from a short[2][3]
memory location. Given that line 6 construct is o.k by 6.7.1§20, this
should mean that line 13 will always compare to 1, whether or not what I
actually meant to be copied is what I got (see #3).
Correct.
3) Will b and/or c contain the array {0,1,2,3,4,5} after lines 9 and 10? My
guess is "yes" as array members need to be contiguous in memory, so arrays
of arrays members (ie.: 1-dim arrays) would also be contiguous in memory as
they are arrays too. However, I'm not sure if there can be padding between
(n-1)dimensional-array elements in (n)dimensional arrays, where n>1, in
which case I would be wrong in my assumption.


Your guess is correct, padding between array members is not allowed. A
multidimensiona l array is really just an array of arrays in C. Padding
is not allowed between the members of an array so there cannot be
padding between the arrays that are themselves members of an array.

Robert Gamble

Nov 15 '05 #3
On Sun, 21 Aug 2005 21:12:58 -0400, Eric Laberge
<de********@myr ealbox.com> wrote:

<snip>
/* 6 */ short a[2][3] = {0,1,2,3,4,5};
/* 7 */ short b[6];
/* 8 */ short c[6];
/* 9 */ memcpy(b, a, sizeof b);
/*10 */ memcpy(c, (int[2][3]){{0,1,2},{3,4, 5}}, sizeof c); <snip> Compiled with "gcc -std=c99 -pedantic -Wall file.c", it however complains
that:
file.c: In function `main':
file.c:6: warning: missing braces around initializer
file.c:6: warning: (near initialization for `a[0]') <snip> Here are a couple of questions:
1) About the compiler warning, either 1) this is not in the official C99
standard and is one of the proposed feature by the commitee or 2) gcc is
wrongly complaining about esthetics (like it suggests double parentheses in
assignations in conditionals), in which case this is off-topic here. Which
one is it?
Yes, partially-braced initializers are standard in both C89/90 and C99
and this gcc warning is just esthetics; whether it's wrong is, like
all esthetics, a matter of taste. DGND. (Aside: we call them
'assignment', 'assignment operator', etc., not 'assignation'.)
2) Are lines 9 and 10 assignments logically equivalent (except of course
that b != c)? My guess is "yes" as both are copying from a short[2][3]
memory location. Given that line 6 construct is o.k by 6.7.1§20, this
should mean that line 13 will always compare to 1, whether or not what I
actually meant to be copied is what I got (see #3).
You actually posted int[2][3] in line 10; _that_ could be different.
If you make it short[2][3] then yes they are the same, except that
your program could make other references/accesses to a, but not to the
compound literal since you don't save/have its address anywhere.
3) Will b and/or c contain the array {0,1,2,3,4,5} after lines 9 and 10? My
guess is "yes" as array members need to be contiguous in memory, so arrays
of arrays members (ie.: 1-dim arrays) would also be contiguous in memory as
they are arrays too. However, I'm not sure if there can be padding between
(n-1)dimensional-array elements in (n)dimensional arrays, where n>1, in
which case I would be wrong in my assumption.

As others have said, yes, arrays are contiguous at all ranks.

- David.Thompson1 at worldnet.att.ne t
Nov 15 '05 #4

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

Similar topics

37
4836
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours, Pujo
4
4636
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
3846
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
6
8147
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
6
4872
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want...
4
17827
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the problem is the charset? How I can avoid this warning? But the worst thing isn't the warning, but that the program doesn't work! The program execute...
5
5968
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a TIFF to several PNG files this causes a problem, becuase the resulting image is (the page to the far left and a lot of black space surrounding it and...
5
5722
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone know a good place to start looking for some theory on the subject of multi user applications? I know only bits and pieces, like about...
0
2302
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing Systems (MuCoCoS'08) Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08. <http://www.par.univie.ac.at/~pllana/mucocos08>...
1
9287
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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...
0
7656
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. ...
0
7808
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...
1
5329
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...
0
4945
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...
0
3450
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...
1
1884
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
0
704
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...

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.