Thread: confusion regarding 2D array of struct pointers

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    26

    confusion regarding 2D array of struct pointers

    I'm trying to construct a little simulation in which I have a 2D World populated by Agents. I'm trying to construct this world as a 2D array of pointers to Agent structs. If no agent is present on a given grid element, that element contains NULL.

    Here is my World typedef:

    Code:
    #include "agent.h"
    
    
    #define WIDTH   100
    #define HEIGHT  100
    
    
    typedef struct _World
    {
        Agent* world[WIDTH][HEIGHT];
        
    }World;
    Agent is another typedef struct.

    I'm doing something or conceptualizing something wrong because when I attempt to compile my code for my World constructor, I get the following error message when I attempt to assign an element of the array to NULL:

    subscripted value is neither an array nor pointer

    I'm confused because my World typedef seems to declare a 2D array of pointers.

    Here is my constructor:

    Code:
    World* world_constructor()
    {
        World* new_world = malloc(sizeof(World));
        int i = 0;
        int j = 0;
        for(i; i < WIDTH; i++)
        {
            j = 0;
            for(j; j < HEIGHT; j++)
            {
                new_world[i][j] = NULL;
            }
        }
        return new_world;
    }
    Thanks for your help.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Right off the top there's not much point making a struct that only contains a single variable.

    Yes the world array is a 2 dimensional array of pointers... but you should know that if you wrote the code.

    Your for() loops need some work... don't declare the initial value externally, use the standard form.

    Also you are not addressing the array, you are addressing the struct, the array is actually at new_world.world

    And, although not a serious problem in a square array you have your height and width dimensions reversed.

    Code:
    // the array
    agent *world[HEIGHT][WIDTH];
    Code:
    // and your loops
    for(i = 0; i <  HEIGHT; i++)
      for (j = 0; j < WIDTH; j++)
        new_world.world[i][j] = NULL;

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    Thanks so much for your advice. I really appreciate well formatted code so thanks for the comments about how to properly format. For some reason, when I stopped writing C++ I started declaring my for loop values outside. I think because you can't declare the variable within the for declaration. Anyway, thanks for the reminder. Also thanks for pointing that out about the height/width flip flop.

    The code you gave works except the dot operator has to be an arrow operator because world is a pointer to a struct:

    Code:
    new_world->world[i][j] = NULL;
    Good point about the struct with only one data member. Silly me.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just so you know... I am not giving you examples for the purpose of letting you copy them and use my code... You need to write your own code. You don't learn a blessed thing if you just copy other peoples work.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    Huh? No way dude. I'm doing this because I love to. I was just stuck and needed some help on one piece of a larger project. The only thing I really used was the line which I corrected and re-posted. Thanks again for the style comments though, again it's really appreciated.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dan_paul View Post
    Huh? No way dude. I'm doing this because I love to. I was just stuck and needed some help on one piece of a larger project. The only thing I really used was the line which I corrected and re-posted. Thanks again for the style comments though, again it's really appreciated.
    No worries... It's just that "untested code" thing that I didn't want you yelling at me for

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    Oh man, all these people giving me free help, don't have the common decency to debug, compile and offer me a finished executable. The nerve!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    LOL... NEVER look a gift horse in the mouth, my friend!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers to a struct
    By Magical in forum C Programming
    Replies: 11
    Last Post: 03-20-2011, 06:03 PM
  2. Struct and array! confusion
    By ronrardin in forum C Programming
    Replies: 5
    Last Post: 04-05-2010, 12:09 PM
  3. array and pointers confusion
    By int80 in forum C Programming
    Replies: 5
    Last Post: 05-02-2008, 03:55 AM
  4. array of pointers of a struct
    By paulur in forum C Programming
    Replies: 18
    Last Post: 04-14-2006, 07:17 AM
  5. array of struct pointers
    By jellyKing in forum C Programming
    Replies: 1
    Last Post: 04-12-2003, 12:17 PM