 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
James Thoni Guest
|
Posted: Tue Nov 15, 2005 2:49 pm Post subject: Pointer to an array |
|
|
How do I define a pointer to a 2 dimensional array, i.e.
int array1[8][6];
int array2[8][6];
int array3[8][6];
int *arraypointer; // ?? pointer to array1,2,or 3
arraypointer = array1; // produces a compiler error
Thanks,
Jim
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Tue Nov 15, 2005 4:34 pm Post subject: Re: Pointer to an array |
|
|
James Thoni wrote:
| Quote: | How do I define a pointer to a 2 dimensional array, i.e.
int array1[8][6];
int array2[8][6];
int array3[8][6];
int *arraypointer; // ?? pointer to array1,2,or 3
arraypointer = array1; // produces a compiler error
|
Please always provide the exact error message.
Depending on how you want to use this pointer, please tell,
you could make it a pointer to the first element of an array.
int *elementpointer = &array1[0][0];
If you increase the pointer it will point to the next element.
Hans.
|
|
| Back to top |
|
 |
liz Guest
|
Posted: Tue Nov 15, 2005 4:43 pm Post subject: Re: Pointer to an array |
|
|
On Tue, 15 Nov 2005 06:49:54 -0800, James Thoni wrote:
| Quote: | int array1[8][6];
int array2[8][6];
int array3[8][6];
int *arraypointer; // ?? pointer to array1,2,or 3
arraypointer = array1; // produces a compiler error
|
course it does.
typedef is your friend.
typedef int T1[8][6];
typedef T1 *PT1 ;
T1 a2;
PT1 pa2;
pa2 = &a2;
--
Good luck,
liz
|
|
| Back to top |
|
 |
Dennis Jones Guest
|
Posted: Tue Nov 15, 2005 5:03 pm Post subject: Re: Pointer to an array |
|
|
"James Thoni" <jim.thoni (AT) worldnet (DOT) att.net> wrote
| Quote: | How do I define a pointer to a 2 dimensional array, i.e.
int array1[8][6];
int array2[8][6];
int array3[8][6];
int *arraypointer; // ?? pointer to array1,2,or 3
arraypointer = array1; // produces a compiler error
|
Well, if you were to read the definition of, "int array1[8][6]" out loud, it
would sound something like this: "array1 is an array of 8 arrays of 6 ints."
So what you need is, "arraypointer is a pointer to an array of 8 arrays of 6
ints."
Now, normally, when you make a pointer to something, you just re-write the
type prepended with an asterisk, like this:
int *arraypointer[8][6] = &array1; // oops, compiler error
Unfortunately, in this case, the asterisk is bound to the 'int', so the
result is exactly the same as:
(int *) arraypointer[8][6] = &array1; // oops, compiler error
which is read as, "arraypointer is an array of 8 arrays of 6 pointers to
int," which is not what you want. To get what you want, you need to make
sure the contents of the array is 'int' (not pointer to int), and to do
that, you have to make sure that the 'int' type is not bound to the asterisk
(which produces a pointer to int). This can be done simply by wrapping
parentheses around the variable name with the asterisk inside the
parentheses, like this:
int (*arraypointer)[8][6] = &array1; // OK
Of course, this assumes you don't care that the size of the array must be
fixed at [8][6]. If you need a pointer that might refer to arrays of
different sizes, you've got a serious problem with your design and may need
to re-think it (for instance, use a pointer to pointer instead of a
two-dimensional array and allocate the pointers on-the-fly (which should be
discouraged),or use a vector of vectors and create a pointer to that).
- Dennis
|
|
| Back to top |
|
 |
Wayne A. King Guest
|
Posted: Tue Nov 15, 2005 8:18 pm Post subject: Re: Pointer to an array |
|
|
On Tue, 15 Nov 2005 06:49:54 -0800, "James Thoni" <jim.thoni (AT) worldnet (DOT) att.net>
wrote:
| Quote: | How do I define a pointer to a 2 dimensional array?
|
You might find ptrtutor.txt helpful, especially Ch. 8:
http://orion.planet.de/~jan/Snippets.9707/_g030a.html
--
Wayne A. King
(waking (AT) idirect (DOT) com, [email]Wayne_A_King (AT) compuserve (DOT) com[/email])
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|