linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "David Chao" <teanau@singnet.com.sg>
To: linux-c-programming@vger.kernel.org
Subject: Something interesting with multi-subscript arrays.
Date: Wed, 26 May 2004 23:30:19 +0800	[thread overview]
Message-ID: <002c01c44336$5d420800$0200a8c0@LAPTOP> (raw)

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


             reply	other threads:[~2004-05-26 15:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-26 15:30 David Chao [this message]
2004-05-26 20:00 ` Something interesting with multi-subscript arrays Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='002c01c44336$5d420800$0200a8c0@LAPTOP' \
    --to=teanau@singnet.com.sg \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).