linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Something interesting with multi-subscript arrays.
@ 2004-05-26 15:30 David Chao
  2004-05-26 20:00 ` Glynn Clements
  0 siblings, 1 reply; 2+ messages in thread
From: David Chao @ 2004-05-26 15:30 UTC (permalink / raw)
  To: linux-c-programming

Hi,

I happen to learn something interesting abt multiple-subscript arrays at
work today. I am hoping if someone can confirm what I discovered. Having
done a fair amt of C programming, I always thought that arrays like:

unsigned char a[2][3];

can be accessed using like that:

unsigned char **b;

b=a;

Then using *(*(b+1)+1) (for eg.) to access the array. Since, isn't this
equivalent to a[1][1]?

However, I am totally wrong! b=a generates a surprise compiler error. I
spent quite sometime to resolve this error. To cut a long story short, I
found another interesting fact. This code:

#include <stdio.h>

int main(void)
{
    unsigned char a[2][3] = {{'a','b','c'},
                             {'d','e','f'}};
    unsigned char (*b)[3];

    b = a;

    printf("%d\n", b);
    printf("%d\n", *b);
    printf("%d\n", **b);
    printf("%c\n", **b);
    system("PAUSE");
    return 0;
}

gives the result:

2293592
2293592
97
a
Press any key to continue . . .

This means that *b does nothing at all in this case. Surprise! Learn
something new everyday.

Best,
David


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-05-26 20:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-05-26 15:30 Something interesting with multi-subscript arrays David Chao
2004-05-26 20:00 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).