Connecting Tech Pros Worldwide Forums | Help | Site Map

Array Initialization

Henry
Guest
 
Posts: n/a
#1: Nov 13 '05
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?



Bigdakine
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Array Initialization


>Subject: Array Initialization[color=blue]
>From: "Henry" henryhc@knology.net
>Date: 9/2/03 3:27 PM Hawaiian Standard Time
>Message-id: <vlagssbqf5q9f3@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?
>[/color]

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"
Nick Austin
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Array Initialization


On Tue, 2 Sep 2003 21:27:36 -0400, "Henry" <henryhc@knology.net>
wrote:
[color=blue]
>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]);[/color]

(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.
[color=blue]
> (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?[/color]

Nick.
Brett Frankenberger
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Array Initialization


In article <vlagssbqf5q9f3@corp.supernews.com>,
Henry <henryhc@knology.net> wrote:[color=blue]
>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]);[/color]

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
Brett Frankenberger
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Array Initialization


In article <20030902214254.22819.00000248@mb-m23.aol.com>,
Bigdakine <bigdakine@aol.comGetaGrip> wrote:[color=blue]
>
>Heck, I'm interested to know how this even compiled.
> (void)printf("%d ", array[x,y]);[/color]

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
Henry
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Array Initialization


> is a comma expression whose value is the value of the second[color=blue]
> 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])[/color]

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


Martin Ambuhl
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Array Initialization


Henry wrote:
[color=blue]
> 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()[/color]

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

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

Irrwahn Grausewitz
Guest
 
Posts: n/a
#8: Nov 13 '05

re: Array Initialization


"Henry" <henryhc@knology.net> wrote in
<vlagssbqf5q9f3@corp.supernews.com>:[color=blue]
>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()[/color]
int main(void)[color=blue]
>{
> 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);[/color]
Get rid of these (void) casts - you don't need them.
[color=blue]
> for(x = 0; x < max_x; x++)
> (void)printf("%d ", array[x,y]);[/color]
^^^^^
Shouldn't this read: array[x][y] ???
[color=blue]
> (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[/color]

Not at all - the initialization is just perfect.[color=blue]
>
>Could someone please help me figure out how this program arrives at such a
>large number?
>[/color]
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.
A. Bolmarcich
Guest
 
Posts: n/a
#9: Nov 13 '05

re: Array Initialization


In article <vlagssbqf5q9f3@corp.supernews.com>, Henry wrote:[color=blue]
> 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?[/color]

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].
Irrwahn Grausewitz
Guest
 
Posts: n/a
#10: Nov 13 '05

re: Array Initialization


Irrwahn Grausewitz <irrwahn@freenet.de> wrote in
<uhialvkvu9u7i0qvgbgss7j0d3t4dtgo0e@4ax.com>:

Arrrrrrrrrrrgh, I did it again, stupid me!
<snip>[color=blue]
>in turn a pointer to an array of five ints, which you print using[/color]
^^^^^^^^^^^^^^^^
<delete this>
<snap>

--
Rain is just liquid sunshine.
Irrwahn Grausewitz
Guest
 
Posts: n/a
#11: Nov 13 '05

re: Array Initialization


"Henry" <henryhc@knology.net> wrote in
<vlaig9e9eht590@corp.supernews.com>:
<SNIP>[color=blue]
>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.[/color]
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?[color=blue]
>
>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.
>[/color]
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.
Jeff
Guest
 
Posts: n/a
#12: Nov 13 '05

re: Array Initialization



"Brett Frankenberger" <rbf@panix.com> wrote in message
news:bj3h6s$gi6$1@reader2.panix.com...[color=blue]
> In article <20030902214254.22819.00000248@mb-m23.aol.com>,
> Bigdakine <bigdakine@aol.comGetaGrip> wrote:[color=green]
> >
> >Heck, I'm interested to know how this even compiled.
> > (void)printf("%d ", array[x,y]);[/color]
>
> 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[/color]

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




Jeff
Guest
 
Posts: n/a
#13: Nov 13 '05

re: Array Initialization



"Henry" <henryhc@knology.net> wrote in message
news:vlaig9e9eht590@corp.supernews.com...[color=blue][color=green]
> > 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])[/color]
>
> 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.[/color]

Burn your book :-)

[snip]

--
Jeff


Henry
Guest
 
Posts: n/a
#14: Nov 13 '05

re: Array Initialization


> > Ok thanks, when I changed the way it was typed it worked much better..[color=blue][color=green]
> > Strangly the array[x,y] came straight from the "Practical C" book.[/color]
>
> Burn your book :-)
>
> [snip]
>
> --
> Jeff[/color]

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


Henry
Guest
 
Posts: n/a
#15: Nov 13 '05

re: Array Initialization



"Irrwahn Grausewitz" <irrwahn@freenet.de> wrote in message
news:62jalvco4mt87i4994jbbam95mi808h42a@4ax.com...[color=blue]
> "Henry" <henryhc@knology.net> wrote in
> <vlaig9e9eht590@corp.supernews.com>:
> <SNIP>[color=green]
> >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.[/color]
> 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?[/color]

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


Joona I Palaste
Guest
 
Posts: n/a
#16: Nov 13 '05

re: Array Initialization


Jeff <nothing@notexist.com> scribbled the following:[color=blue]
> "Brett Frankenberger" <rbf@panix.com> wrote in message
> news:bj3h6s$gi6$1@reader2.panix.com...[color=green]
>> In article <20030902214254.22819.00000248@mb-m23.aol.com>,
>> Bigdakine <bigdakine@aol.comGetaGrip> wrote:[color=darkred]
>> >
>> >Heck, I'm interested to know how this even compiled.
>> > (void)printf("%d ", array[x,y]);[/color]
>>
>> 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.[/color][/color]
[color=blue]
> 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[/color]
[color=blue]
> array[ printf("Hello man") , y][/color]
[color=blue]
> To OP :
> It dosen't mean "array[x,y] " is the correct syntax for accessing
> two-dimensional
> array. You should write "array[x][y]".[/color]

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 (palaste@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
Martien Verbruggen
Guest
 
Posts: n/a
#17: Nov 13 '05

re: Array Initialization


On Tue, 2 Sep 2003 23:10:21 -0400,
Henry <henryhc@knology.net> wrote:[color=blue]
>
> "Irrwahn Grausewitz" <irrwahn@freenet.de> wrote in message
> news:62jalvco4mt87i4994jbbam95mi808h42a@4ax.com...[color=green]
>> "Henry" <henryhc@knology.net> wrote in
>> <vlaig9e9eht590@corp.supernews.com>:
>> <SNIP>[color=darkred]
>> >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.[/color]
>> 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?[/color]
>
> Yes... it seemed strange to me that they would expect a return on printf[/color]

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
Jens.Toerring@physik.fu-berlin.de
Guest
 
Posts: n/a
#18: Nov 13 '05

re: Array Initialization


Henry <henryhc@knology.net> wrote:[color=blue][color=green][color=darkred]
>> > 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.[/color]
>>
>> Burn your book :-)[/color][/color]
[color=blue]
> 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[/color]

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
--
_ _____ _____
| ||_ _||_ _| Jens.Toerring@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Irrwahn Grausewitz
Guest
 
Posts: n/a
#19: Nov 13 '05

re: Array Initialization


"Henry" <henryhc@knology.net> wrote in
<vlamrim0cu7ec3@corp.supernews.com>:
[color=blue][color=green][color=darkred]
>> > 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.[/color]
>>
>> Burn your book :-)
>>
>> [snip]
>>
>> --
>> Jeff[/color]
>
>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[/color]

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.
Stuart
Guest
 
Posts: n/a
#20: Nov 13 '05

re: Array Initialization


rbf@panix.com (Brett Frankenberger) wrote in message news:<bj3h6s$gi6$1@reader2.panix.com>...[color=blue]
> In article <20030902214254.22819.00000248@mb-m23.aol.com>,
> Bigdakine <bigdakine@aol.comGetaGrip> wrote:[color=green]
> >
> >Heck, I'm interested to know how this even compiled.
> > (void)printf("%d ", array[x,y]);[/color]
>
> 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[/color]

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
Closed Thread