linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David McMurray <david@davidmcmurray.net>
To: linux-c-programming@vger.kernel.org
Subject: Re: array pointer
Date: Tue, 18 Jan 2011 16:59:44 +0000	[thread overview]
Message-ID: <AANLkTikhBuoMeq+Ayt0kd=nR6=zJvE9mDzqX-eUF_n+1@mail.gmail.com> (raw)
In-Reply-To: <op.vpid0asz3l0zgt@mnazarewicz.zrh.corp.google.com>

>> int  *s_ptr;
>> int  **d_ptr;
>> int arr[2][2]={1,2,3,4};
>
> One image is worth a thousand words, so here it goes:
>
> arr is something like this in memory:
>
> +---+---+---+---+
> | 1 | 2 | 3 | 4 |
> +---+---+---+---+
>
> d_ptr would require something like:
>
> +---------+---------+
> | pointer | pointer |
> +----|----+----|----+
>     |         |     +---+---+
>     |         +---> | 3 | 4 |
>     |     +---+---+ +---+---+
>     +---> | 1 | 2 |
>           +---+---+
>
> Ie. in arr all the numbers are lied linearly in memory whereas
> int **d_ptr is an array of pointers to array of ints.

I would say int **d_ptr is really a pointer to a pointer and not
necessarily an array of pointers.

So, the following is fine:
d_ptr = &s_ptr;
printf("s_ptr: %d, d_ptr: %d\n", *s_ptr, **d_ptr);

and displays "s_ptr: 1, d_ptr: 1"

To (attempt to) answer your original question though, the problem is
that "arr" will give you the address of the array, not the address of
a pointer to the array. For each extra dimension of the array you need
to add a * when referring to the name of the array without indices in
order to de-reference the additional dimensions.

So,
1-dimensional array
int *s_ptr;
int arr[2]={1,2};
s_ptr = arr; /* Ok because arr references the address of the array
immediately */

2-dimensional array
int *s_ptr;
in arr[2][2]={1,2,3,4};
s_ptr = *arr; /* Ok because the * de-references arr to the address of
the array, but arr is not the address of a pointer to the array */

3-dimensional array
int *s_ptr;
int arr[2][2][2]={1,2,3,4,5,6,7,8};
s_ptr = **arr; /* Ok, again each * de-references additional dimensions
of the array */

But these references are not pointers, which is why int **d_ptr; int
arr[2][2]={1,2,3,4}; d_ptr = arr; will not work, arr will not be a
pointer to a pointer.

HTH,
David McMurray.
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2011-01-18 16:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-18 13:22 array pointer ratheesh k
2011-01-18 13:23 ` ratheesh k
2011-01-18 13:32   ` ratheesh k
2011-01-18 14:46     ` Michal Nazarewicz
2011-01-18 16:59       ` David McMurray [this message]
2011-01-18 19:33     ` 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='AANLkTikhBuoMeq+Ayt0kd=nR6=zJvE9mDzqX-eUF_n+1@mail.gmail.com' \
    --to=david@davidmcmurray.net \
    --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).