|
-
Apr 5th, 2002, 02:39 AM
#1
Thread Starter
Hyperactive Member
pointers in C
how do i point to a 2 dimensional array in C?
int array[3][3] = { {1,2,3} ,
{3,2,1},
{1,2,3}};
int *ptArray;
ptArray = array;
if pointing to a 1 dimensional array i would use *(ptArray+2) to point to the 3rd element of a 1D array.
how can i point to a 2D array lets say array[3][1] from the above example?
Thanks
i am just starting to learn C
-
Apr 5th, 2002, 06:56 AM
#2
New Member
You can't actually use a pointer to point to a 2-D array. You have to use a pointer to a pointer ( **) That means that you use one pointer(*) to point to the row of the 2-D array and use another pointer(**) to point to the column of the 2-D array.
-
Apr 5th, 2002, 07:12 AM
#3
Thread Starter
Hyperactive Member
can you show me an example of 2 pointers pointing to a 2D array?
is this the so called "double pointers"?
-
Apr 6th, 2002, 05:56 PM
#4
It's better to use a single pointer to point to an array and use this simple formula to calculate the 1d offset:
(an array with n dimensions labeled a, b, c...)
offset = a + size(a)*b + size(b)*c + ...
where size(x) means: the offsets this dimension occupies, in case of an array
ar[5][6][7];
it is
size(a) = 5
size(b) = 30 (5*6)
size(c) = 210 (5*6*7)
Pointers to pointers soon get confusing!
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|