Re: Array Initialization
Jeff <nothing@notexi st.com> scribbled the following:[color=blue]
> "Brett Frankenberger" <rbf@panix.co m> wrote in message
> news:bj3h6s$gi6 $1@reader2.pani x.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.hel sinki.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
Jeff <nothing@notexi st.com> scribbled the following:[color=blue]
> "Brett Frankenberger" <rbf@panix.co m> wrote in message
> news:bj3h6s$gi6 $1@reader2.pani x.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.hel sinki.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
Comment