473,804 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array Initialization

I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main()
{
int x,y;

for(x = 0; x < max_x; x++)
for(y=0; y < max_y; y++)
array[x][y] = x * 10 + y;

for(y = 0; y < max_y; y++) {
(void)printf("a rray[%d] ", y);
for(x = 0; x < max_x; x++)
(void)printf("% d ", array[x,y]);
(void)printf("\ n");
}

return 0;
}

This program initializes to :
array[0] 56164 56164 56164
array[1] 56184 56184 56184
array[2] 56204 56204 56204
array[3] 56224 56224 56224
array[4] 56244 56244 56244

Could someone please help me figure out how this program arrives at such a
large number?
Nov 13 '05 #1
19 4563
>Subject: Array Initialization
From: "Henry" he*****@knology .net
Date: 9/2/03 3:27 PM Hawaiian Standard Time
Message-id: <vl************ @corp.supernews .com>

I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main()
{
int x,y;

for(x = 0; x < max_x; x++)
for(y=0; y < max_y; y++)
array[x][y] = x * 10 + y;

for(y = 0; y < max_y; y++) {
(void)printf("a rray[%d] ", y);
for(x = 0; x < max_x; x++)
(void)printf("% d ", array[x,y]);
(void)printf("\ n");
}

return 0;
}

This program initializes to :
array[0] 56164 56164 56164
array[1] 56184 56184 56184
array[2] 56204 56204 56204
array[3] 56224 56224 56224
array[4] 56244 56244 56244

Could someone please help me figure out how this program arrives at such a
large number?


Heck, I'm interested to know how this even compiled.

(void)printf("% d ", array[x,y]);

Should be .. array[x][y]....

Fortran dies hard, don't it? :-)

Stuart
Dr. Stuart A. Weinstein
Ewa Beach Institute of Tectonics
"To err is human, but to really foul things up
requires a creationist"
Nov 13 '05 #2
On Tue, 2 Sep 2003 21:27:36 -0400, "Henry" <he*****@knolog y.net>
wrote:
I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main()
{
int x,y;

for(x = 0; x < max_x; x++)
for(y=0; y < max_y; y++)
array[x][y] = x * 10 + y;

for(y = 0; y < max_y; y++) {
(void)printf("a rray[%d] ", y);
for(x = 0; x < max_x; x++)
(void)printf("% d ", array[x,y]);
(void)printf("% d ", array[x][y]);

array[x,y] uses the comma operator and is the same as array[y].

You're invoking UB because array[y] has type 'int *' and
you're printing it with %d.
(void)printf("\ n");
}

return 0;
}

This program initializes to :
array[0] 56164 56164 56164
array[1] 56184 56184 56184
array[2] 56204 56204 56204
array[3] 56224 56224 56224
array[4] 56244 56244 56244

Could someone please help me figure out how this program arrives at such a
large number?


Nick.
Nov 13 '05 #3
In article <vl************ @corp.supernews .com>,
Henry <he*****@knolog y.net> wrote:
I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

int array[max_x][max_y];

(void)printf("% d ", array[x,y]);


array[x,y] is not the same thing as array[x][y].
The expression:
x,y
is a comma expression whose value is the value of the second
expression (in this case, y). The first expression (in this case, x)
would be evaluated for its side effects; but in this case, there are
none. So x,y is equivalent to just:
y

So you effectively have
printf("%d ", array[y])

array[y] is type (pointer to int). "%d" requires an int. So you're
using an "int" format specifier to print a (pointer to int); the result
is undefined. Change the code to:
printf("%d ", array[x][y]);
and you'll probably get what you expected.

By the way, I'm curious what platform you are running on. (i.e. what
compiler and what OS and what processor)? I'm somewhat surprised at
the numbers you got. (It's all undefined, of course ... but on a
typical PC platform, I'd have expected the actual result to be much
larger than the numbers you got.)

-- Brett
Nov 13 '05 #4
In article <20************ *************** @mb-m23.aol.com>,
Bigdakine <bi*******@aol. comGetaGrip> wrote:

Heck, I'm interested to know how this even compiled.
(void)printf("% d ", array[x,y]);


Why wouldn't it? 'x,y' is a perfectly valid expression (equivalent to
'y'), and array[y] doesn't violate any constraints (and would even be
useful, defined behavior under some circumstances), so one wouldn't
expect compilation to fail. Of course, array[y] is type (pointer to
int), so attempting to print it with "%d" is undefined behavior.

-- Brett
Nov 13 '05 #5
> is a comma expression whose value is the value of the second
expression (in this case, y). The first expression (in this case, x)
would be evaluated for its side effects; but in this case, there are
none. So x,y is equivalent to just:
y

So you effectively have
printf("%d ", array[y])


Ok thanks, when I changed the way it was typed it worked much better..
Strangly the array[x,y] came straight from the "Practical C" book.

The new initialization is this:
array[0] 0 10 20
array[1] 1 11 21
array[2] 2 12 22
array[3] 3 13 23
array[4] 4 14 24

This seems like a much more plausible answer.. Although I am still a little
confused as to how it arrives at the numbers in that order. Could someone
explain it to me a little clearer? The book is not doing a very good job.

Thanks
Nov 13 '05 #6
Henry wrote:
I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main()
For goodness' sake! Why would anyone use an implicit int as the return
value for main, and then decorate printf calls with casts?
[...]
(void)printf("a rray[%d] ", y);


If you do this to satisfy some version of lint, get one that knows that not
only is an explicit 'int' on main a good idea, but it is required by the
current C standard.

--
Martin Ambuhl

Nov 13 '05 #7
"Henry" <he*****@knolog y.net> wrote in
<vl************ @corp.supernews .com>:
I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main() int main(void){
int x,y;

for(x = 0; x < max_x; x++)
for(y=0; y < max_y; y++)
array[x][y] = x * 10 + y;

for(y = 0; y < max_y; y++) {
(void)printf("a rray[%d] ", y); Get rid of these (void) casts - you don't need them.
for(x = 0; x < max_x; x++)
(void)printf("% d ", array[x,y]); ^^^^^
Shouldn't this read: array[x][y] ???
(void)printf("\ n");
}

return 0;
}

This program initializes to :
array[0] 56164 56164 56164
array[1] 56184 56184 56184
array[2] 56204 56204 56204
array[3] 56224 56224 56224
array[4] 56244 56244 56244
Not at all - the initialization is just perfect.
Could someone please help me figure out how this program arrives at such a
large number?

You print array[x,y], which is equivalent to array[y], which is
in turn a pointer to an array of five ints, which you print using
the %d format specifier - hence the 'strange' values.

Irrwahn
--
Rain is just liquid sunshine.
Nov 13 '05 #8
In article <vl************ @corp.supernews .com>, Henry wrote:
I finally thought I had an understanding of multi dimensional arrays in C
when I get this:

#include <stdio.h>

#define max_x 3
#define max_y 5

int array[max_x][max_y];

main()
{
int x,y;

for(x = 0; x < max_x; x++)
for(y=0; y < max_y; y++)
array[x][y] = x * 10 + y;

for(y = 0; y < max_y; y++) {
(void)printf("a rray[%d] ", y);
for(x = 0; x < max_x; x++)
(void)printf("% d ", array[x,y]);
(void)printf("\ n");
}

return 0;
}

This program initializes to :
array[0] 56164 56164 56164
array[1] 56184 56184 56184
array[2] 56204 56204 56204
array[3] 56224 56224 56224
array[4] 56244 56244 56244

Could someone please help me figure out how this program arrives at such a
large number?


The statement

(void)printf("% d ", array[x,y]);

prints the value of array[y] because the value of expression x,y is y.
You likely meant to write

(void)printf("% d ", array[x][y]);

Because the array is a two dimensional array, the value of array[y] is a
pointer to a one-dimensional array. The values being printing in each
row are &array[0][0], &array[1][0], &array[2][0], &array[3][0], and
&array[4][0].
Nov 13 '05 #9
Irrwahn Grausewitz <ir*****@freene t.de> wrote in
<uh************ *************** *****@4ax.com>:

Arrrrrrrrrrrgh, I did it again, stupid me!
<snip>
in turn a pointer to an array of five ints, which you print using

^^^^^^^^^^^^^^^ ^
<delete this>
<snap>

--
Rain is just liquid sunshine.
Nov 13 '05 #10

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

Similar topics

2
5580
by: Fred Zwarts | last post by:
If I am right, members of a class that are const and not static must be initialized in the initialization part of a constructor. E.g. class C { private: const int I; public: C(); };
13
27189
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
8
3691
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
15
4907
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with memset(). E.g., static struct mystruct_st { int x1;
3
1983
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value initialization when the type is instantiated. Any suggestions for a mod that would allow array initialization syntax? ***********Here's the type: #ifndef CHECKED_ARRAY_ #define CHECKED_ARRAY_
5
24324
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a default ctor). Is it possible to do so in the class parameter initialization using specific ctor?
15
3381
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
2
2181
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
152
9931
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
0
9579
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
10571
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
10326
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...
1
10317
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
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
7615
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
6851
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
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.