# Pointer Syntax

• 09-12-2002
monkey_C
Pointer Syntax
I've got a card dealing program and I want to convert all the array syntax into pointer syntax. Whereas in array syntax I would just use the element's subscript (i.e. arr[3] ) in pointer syntax I need to use the address of the first element and add to it the number of bytes of memory I want to move over (i.e. *(arr+3) ). I know this is the long way around but I need a way to do this for a multi-dimensional array.

How can I turn this into pointer syntax?

wDeck[row][column]
• 09-12-2002
Hammer
Here's some good reading for you to get started with...
• 09-12-2002
Magos
Array[X + (Y * MaxX)]

X = column
Y = row
MaxX = Maximum number of columns available
• 09-12-2002
monkey_C
Thank You Magos! I knew that formula existed I just couldn't figure it out. Again thanks!
• 09-12-2002
moi
Quote:

Originally posted by Magos
Array[X + (Y * MaxX)]

X = column
Y = row
MaxX = Maximum number of columns available

/me winces

did you try compiling this? didn't think so. used with his array wDeck[row][column], this yields a pointer to an integer (NOT an integer) that is out-of-bounds (i.e. not in your memory area). the real answer is here.

and if you're curious, i wasn't 100% sure about my answer either, so i tried compiling it and guess what? it worked.

edit: btw i don't expect anyone to try a test compile before they answer every question, but if you're not quite sure wth you are doing, then, yeah.
• 09-12-2002
Magos
Quote:

Originally posted by moi
/me winces

did you try compiling this? didn't think so. used with his array wDeck[row][column], this yields a pointer to an integer (NOT an integer) that is out-of-bounds (i.e. not in your memory area). the real answer is here.

and if you're curious, i wasn't 100% sure about my answer either, so i tried compiling it and guess what? it worked.

edit: btw i don't expect anyone to try a test compile before they answer every question, but if you're not quite sure wth you are doing, then, yeah.

What?
Assume that MaxX = MaxY = 8, then the available memory in the array is 8 * 8 = 64.
X and Y will range from 0 - 7.
Assuming the "worst-case", that X and Y is as large as they can be, then my formula will produce the number 7 + (7 * 8) = 63, which is within the bounds (the last element in fact).
• 09-13-2002
moi
Quote:

Originally posted by Magos
What?

What? It doesn't work! That's what.

edit: apparently the person who asked this question wrote two threads about it (not a good idea), and i'm interchanging information between the two threads. the reason your way doesnt work here is because of the way he's using the array.
Quote:

Originally posted by monkey_C
OK, wDeck was originally declared in main() as

int deck [4] [13] = {0};

and was passed into the function containing my problematic line as

const int wDeck[][13]

but I still don't see how that's affecting my code. I think I've just been staring at the same line for too long.

check out http://cboard.cprogramming.com/showt...threadid=24797 for the whole dealie. because he's passing it as a two dimensional array, trying to access it as a one dimensional array wont work unless you something like
(&wDeck[0][0])[X + (Y * MaxX)]

or pass a *int in the first place, or do something like hammer outlined. but this is all very redundant and useless. monkeyc, care to elaborate why it must be this way?
• 09-13-2002
The Dog
Try this:

Code:

```myType  wDeck[50][50];    //for arguments sake myType* temp; temp = (wDeck + (y * MaxX + x)); //MaxX is a constant```
y is the first subscript and x is the second.

so to access element wDeck[1][10]; which is the 61st element, y would be 50, which gives us 50, plus x which is 10, gives us 60,
added to wDeck reveals the 61st element.

I haven't tried it but it look's sound to me.