Pointer question
slewis at oregon.uoregon.edu
slewis at oregon.uoregon.edu
Thu May 3 02:52:56 AEST 1990
Hello netters.
I have a question about pointers. Well, actually I have a few observations
and I'm not sure if they're correct. Maybe you can help...
Assume the following variables:
int i,j;
int **p, *a, *b;
Also assume that we have allocated space for a 2D n X m int "array" (p),
a n-length linear array (a) and an m-length linear array (b) in the
following manner:
p = (int **) malloc(n*sizeof(int *));
for(i=0; i < n; i++) p[i] = (int *) malloc(m*sizeof(int));
and
a = (int *) malloc(n*sizeof(int));
b = (int *) malloc(m*sizeof(int));
Now, we may use p[i][j], a[i] and b[i] to refer to the elements of these
arrays.
My understanding is that a[i] is equiv to *(a+i). Now, this
implies some strange things, right? First, since (pointer) addition is
commutative, a[i] == *(a+i) == i[a]. I've seen this stated before in
this newsgroup.
But are the following assertions true:
1. p[i][j] == *( *(p+i) + j) == j[p[i]] == j[i[p]]
2. a[b[i]] == *( a + *(b+i)) == b[i][a] == i[b][a]
(assuming that b[i] contains a valid [-1 < b[i] < n] number)
If so, I think I understand. If not, I'm confused.
Scott
More information about the Comp.lang.c
mailing list