By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

Array Initialization

P: n/a
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("array[%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
Share this Question
Share on Google+
19 Replies


P: n/a
>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("array[%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

P: n/a
On Tue, 2 Sep 2003 21:27:36 -0400, "Henry" <he*****@knology.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("array[%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

P: n/a
In article <vl************@corp.supernews.com>,
Henry <he*****@knology.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

P: n/a
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

P: n/a
> 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

P: n/a
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("array[%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

P: n/a
"Henry" <he*****@knology.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("array[%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

P: n/a
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("array[%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

P: n/a
Irrwahn Grausewitz <ir*****@freenet.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

P: n/a
"Henry" <he*****@knology.net> wrote in
<vl************@corp.supernews.com>:
<SNIP>
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. If there are more flaws like this in the book, get a better one.
(Btw.: were the (void) casts for printf straight from that book, too?
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.

You initialize the array like that:

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

and print it out this way:

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

and this is what you get:
x
0 1 2
y +---------
0| 0 10 20
1| 1 11 21
2| 2 12 22
3| 3 13 23
4| 4 14 24

Your confusion may rise from the fact that you arranged the loops
in different ways for the initialization and the output, but that
changes absolutely nothing - well, as long as you use x and y in
a consistent manner, which you perfectly did.

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

P: n/a

"Brett Frankenberger" <rb*@panix.com> wrote in message
news:bj**********@reader2.panix.com...
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


Yes, it can be compiled. The comma between a and b is "comma operator".
The expression "a,b" will return "b" silently. You can even write

array[ printf("Hello man") , y]
To OP :
It dosen't mean "array[x,y] " is the correct syntax for accessing
two-dimensional
array. You should write "array[x][y]".

--
Jeff


Nov 13 '05 #12

P: n/a

"Henry" <he*****@knology.net> wrote in message
news:vl************@corp.supernews.com...
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.


Burn your book :-)

[snip]

--
Jeff
Nov 13 '05 #13

P: n/a
> > 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.


Burn your book :-)

[snip]

--
Jeff


LOL

Well I started learning C using the K&R book but it assumed too much, so I
was told "Practical C" would be a much better book to go through because it
would be more in depth and would cover topics that K&R would seemingly skip
over. So far "PC" has been compeltly different.. using commands such as
scanf and fgets which K&R didn't even mention
Nov 13 '05 #14

P: n/a

"Irrwahn Grausewitz" <ir*****@freenet.de> wrote in message
news:62********************************@4ax.com...
"Henry" <he*****@knology.net> wrote in
<vl************@corp.supernews.com>:
<SNIP>
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.

If there are more flaws like this in the book, get a better one.
(Btw.: were the (void) casts for printf straight from that book, too?


Yes... it seemed strange to me that they would expect a return on printf
Nov 13 '05 #15

P: n/a
Jeff <no*****@notexist.com> scribbled the following:
"Brett Frankenberger" <rb*@panix.com> wrote in message
news:bj**********@reader2.panix.com...
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.

Yes, it can be compiled. The comma between a and b is "comma operator".
The expression "a,b" will return "b" silently. You can even write array[ printf("Hello man") , y] To OP :
It dosen't mean "array[x,y] " is the correct syntax for accessing
two-dimensional
array. You should write "array[x][y]".


However, due to C's wacky ways of working, the following code is
perfectly legal:

#include <stdio.h>
int main(void) {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf("%d\n", array[printf("Hello man\n")]);
return 0;
}

This should print out:
Hello man
11

Because "Hello man\n" is 10 printable characters (and a '\0'), and
array[10] is 11.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 13 '05 #16

P: n/a
On Tue, 2 Sep 2003 23:10:21 -0400,
Henry <he*****@knology.net> wrote:

"Irrwahn Grausewitz" <ir*****@freenet.de> wrote in message
news:62********************************@4ax.com...
"Henry" <he*****@knology.net> wrote in
<vl************@corp.supernews.com>:
<SNIP>
>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.

If there are more flaws like this in the book, get a better one.
(Btw.: were the (void) casts for printf straight from that book, too?


Yes... it seemed strange to me that they would expect a return on printf


That's not strange, since printf() does return a value. Explicitly
casting it away like that, however, isn't really necessary. It used to
be done a lot to shut up warnings from lint about discarded return
values, but it isn't necessary to do.

Martien
--
|
Martien Verbruggen | The problem with sharks is that they are too
| large to get to the shallow end of the gene
| pool. -- Scott R. Godin
Nov 13 '05 #17

P: n/a
Henry <he*****@knology.net> wrote:
> 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.
Burn your book :-)

Well I started learning C using the K&R book but it assumed too much, so I
was told "Practical C" would be a much better book to go through because it
would be more in depth and would cover topics that K&R would seemingly skip
over. So far "PC" has been compeltly different.. using commands such as
scanf and fgets which K&R didn't even mention


You better have a another look at your K&R2 (I hope you have the second
edition), and go to section 7.4 (completely devoted to scanf()) and 7.7
(discusses fgets() and fputs()). K&R probably doesn't introduce these
functions earlier because you need to have understood about pointers
and arrays before you can use them. I guess that a lot of the questions
you see asked in clc why stuff like

char *my_string;
double my_float;
scanf( "%f %s", my_float, my_string );

does not work may be due to books introducing scanf() too early...

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #18

P: n/a
"Henry" <he*****@knology.net> wrote in
<vl************@corp.supernews.com>:
> 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.


Burn your book :-)

[snip]

--
Jeff


LOL

Well I started learning C using the K&R book but it assumed too much, so I
was told "Practical C" would be a much better book to go through because it
would be more in depth and would cover topics that K&R would seemingly skip
over. So far "PC" has been compeltly different.. using commands such as
scanf and fgets which K&R didn't even mention


Huh? You must own a very rare preliminary version of K&R! :)

In my copy of K&R2 fgets and (f)scanf are used frequently in
the examples. scanf has a section dedicated to it as well:
Chapter 7.4: Formatted Input - Scanf
And in 7.7 the source code for a possible fgets/fputs
implementation is given...
--
No sig today.
Nov 13 '05 #19

P: n/a
rb*@panix.com (Brett Frankenberger) wrote in message news:<bj**********@reader2.panix.com>...
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


I forgot about the "," operator.

Thanx. Be that as it may, if the intent is to print an element of a
2-d array, it should be written as [x][y]

Stuart
Nov 13 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.