Thread: Pointer Syntax

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    Question 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]
    don't be a two-bit user

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's some good reading for you to get started with...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Array[X + (Y * MaxX)]

    X = column
    Y = row
    MaxX = Maximum number of columns available
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    Thank You Magos! I knew that formula existed I just couldn't figure it out. Again thanks!
    don't be a two-bit user

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.
    Last edited by moi; 09-12-2002 at 06:21 PM.
    hello, internet!

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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).

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.
    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?
    Last edited by moi; 09-13-2002 at 03:44 AM.
    hello, internet!

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.
    Last edited by The Dog; 09-13-2002 at 03:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PlaySound
    By cangel in forum C++ Programming
    Replies: 16
    Last Post: 10-08-2009, 05:29 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM