Array Initialization

H

Henry

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?
 
B

Bigdakine

Subject: Array Initialization
From: "Henry" (e-mail address removed)
Date: 9/2/03 3:27 PM Hawaiian Standard Time
Message-id: <[email protected]>

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"
 
N

Nick Austin

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.
 
B

Brett Frankenberger

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
 
B

Brett Frankenberger

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
 
H

Henry

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
 
M

Martin Ambuhl

Henry said:
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.
 
I

Irrwahn Grausewitz

Henry said:
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
 
A

A. Bolmarcich

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].
 
I

Irrwahn Grausewitz

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.
 
J

Jeff

Brett Frankenberger said:
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]".
 
J

Jeff

Henry said:
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]
 
H

Henry

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]

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
 
H

Henry

Irrwahn Grausewitz said:
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
 
J

Joona I Palaste

Jeff said:
Brett Frankenberger said:
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 ([email protected]) ---------------------------\
| 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
 
M

Martien Verbruggen

Irrwahn Grausewitz said:
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
 
J

Jens.Toerring

Henry said:
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
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
I

Irrwahn Grausewitz

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]

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...
 
S

Stuart

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,078
Messages
2,570,572
Members
47,204
Latest member
MalorieSte

Latest Threads

Top