Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers and Multidimensional arrays

Status
Not open for further replies.

krsnabansi

Programmer
May 12, 2002
1
IN
Hello,

Of late, I have been reading about using pointer notation with multidimensional arrays. I have the following doubts:
1. It is easy to undertand the memory allocation pattern in the case of one-dimensional array. For example,
double values[10];
double* pvalues = values;
in the above expression, values is the address of the first element of the array.
But, I am pretty confused about the memory allocation pattern in the case of multidimensional arrrays. For example,
double values[j];
Now, if I need to declare a pointer to the array, I can't do it as
double* pvalues = values:/*wrong!!!*/
instead I should declare it as
double *(pvalues)[j] = values;
Could anyone explain the logic behind this?

2. If I have to access the element in a one-dimensional array using pointer notation, I can do as follows:
(pvalues + i);/*assuming pvalues = values*/

But to achieve the same in the case of a multi-dimensional array, I have to write the following expression:
(*(values + i) + j)
But in both the cases, values refers to the address of the first element.

Could anyone explain?

thanks
KB
 
1.
/********************************************************/
double values[j];
Now, if I need to declare a pointer to the array, I can't do it as
double* pvalues = values /* not wrong!!!*/
/*******************************************************/

I've tried it and it works fine with me :) .

2.
In memory a multidimensional array like int arr[3][4]
would be like
|aaaabbbbcccc|

so if I wanted the second element of the second line
(arr[1][1]), I would type (arr+4+1) or
arr+i*number+secondnumber
with number being 4 and secondnumber being 1 here
(because the first element is 0,the second is 1).
wich makes arr+1*4+1 or arr+5.
or
int* m=arr;
int a=*(m+5);


Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
double value1;
double value2 [j];
double *p1 = value1; <- pointer to double.
double *(p2)[j] = value2; <- pointer to array of j doubles;
Now the difference:

p1+1 -> will make a shift in memory to size of one double and we will have pointer to second element of array
value1[1].
p2+1 will make shift in memory to size of j doubles and we will have acces to second row of 2dim array
value2[1][0].

if you do this p1=value2; (first you get error or warning during compilation about type missmutch) then call
p1+1 will get you access to value2[0][1] not a value2[1][0]

About second question:
Compiler read a[2] as *(a + 2). BTW 2[a] will also work because 2[a] will be treated as *(2+a) the same as *(a+2).
So we can access values as:
double *(p2)[j]=value2;
value2[n][m] = *(*(value2 + n) + m) = *((p2+n)+m);
or pointer to i-row:
value2 = *(value + i) = p2 + i;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top