Thread: Help with 2d array of structures and pointers.

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Help with 2d array of structures and pointers.

    Hello everyone. I need a little help with my assignment trying to get pointers to reference a 2d array of structures.

    Here's the original code:
    Code:
    //this is global
    struct structFractal{
        uchar R, G, B;
    } fractal[ySize][xSize];
    A function goes through a bunch of for loops to generate the fractal:

    Code:
                fractal[y][x].R = LUT[color].R;
                fractal[y][x].G = LUT[color].G;
                fractal[y][x].B = LUT[color].B;
    It compiles and works fine with this, but i like i said i have to change it so it uses pointers to reference the arrays... I'm lost.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Sounds like a homework project. Let's see your best attempt.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    2
    I'm think i got it actually. This is what I ended up with:
    Code:
    //global
    struct structFractal{
        uchar R, G, B;
    } fractal[ySize][xSize];
    ...
    Code:
    //code in function
    struct structFractal *ptrFractal;
    ptrFractal = &fractal[0][0];
    //For loops...
           (ptrFractal + y * ySize + x)->R  = LUT[color].R;
           (ptrFractal + y * ySize + x)->G = LUT[color].G;
           (ptrFractal + y * ySize + x)->B = LUT[color].B;
    How's it look?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array of structures
    By phoneix_hallows in forum C Programming
    Replies: 3
    Last Post: 08-27-2009, 11:13 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. 2d array of structures
    By spudtheimpaler in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 03:17 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 03:26 PM