C: 2D arrays with structs and pointers

In summary, the speakers are discussing a potential variant on a 2D map game where the player's position is represented by a 2D array filled with NULL pointers. They are also discussing a struct that holds pointers to different creatures on the map. The question at hand is how to access the struct from a specific position in the array. The expert advises using the "->" operator to reference a member of a pointer to a struct, and to change the type casting and allocation to properly reference the variable name.
  • #1
Peter P.
23
0
I wanted to make a variant on having a player just walking around a small 2d map. Instead of having 2 variables to store the current x and y values of the player position in a certain array, I want to make a 2d array that is filled with NULL pointers. Then the player would be represented as a struct and instead of changing the x and y variables around, I would change the pointers around. I just want to know how you access something like this. Here is my current code:

Code:
#include <curses.h>
#include <stdlib.h>

#define NUM_ROWS 20
#define NUM_COLS 20

#define WALL 0
#define FLOOR 1
#define PASS 1
#define NOT_PASS 0

typedef struct playerChar {
    int HP;
} PlayerChar;

//General struct with pointers to every type of 'creature' that can be shown on map
typedef struct charPointers {
    PlayerChar * pPointer;
} CharPointers;

int main (void) {
    initscr();
    noecho();
    curs_set(0);
    resize_term (NUM_ROWS, NUM_COLS);

    int i, j;
    int ** mapArray = (int **) malloc (NUM_ROWS * sizeof (int *));
    int ** passableArray = (int **) malloc (NUM_ROWS * sizeof (int *));
    int ** charLoc = (int **) malloc (NUM_ROWS * sizeof (int *));


    for (i = 0; i < NUM_ROWS; i++) {
        mapArray [i] = (int *) malloc (NUM_COLS * sizeof (int));
        passableArray [i] = (int *) malloc (NUM_COLS * sizeof (int));
        charLoc [i] = (int *) malloc (NUM_COLS * sizeof (CharPointers));
        for (j = 0; j < NUM_COLS; j++) {
            if (i == 0) {
                mapArray [i][j] = WALL;
                passableArray [i][j] = NOT_PASS;
            }
            else if (i == (NUM_ROWS - 1)) {
                mapArray [i][j] = WALL;
                passableArray [i][j] = NOT_PASS;
            }
            else if (j == 0) {
                mapArray [i][j] = WALL;
                passableArray [i][j] = NOT_PASS;
            }
            else if (j == (NUM_COLS - 1)) {
                mapArray [i][j] = WALL;
                passableArray [i][j] = NOT_PASS;
            }
            else {
                mapArray [i][j] = FLOOR;
                passableArray [i][j] = PASS;
            }
            charLoc[i][j].CharPointers.pPointer = NULL;
        }
    }
The second struct I have there currently is just to use to hold all of the possible "monster" pointers. Doesn't really do much right now, but that's its intended purpose in the future.

My question deals with the line: "charLoc[j].CharPointers.pPointer = NULL;". I don't know how I would access something like the struct from a certain position in the array, and then from the struct to the other structs.

Thanks in advance.
 
Technology news on Phys.org
  • #2
When referencing a member of a pointer to a struct use the -> operator.
 
  • #3
But how would i use that in this context? I know that i can do that for when I'm going from pointers to structs, but what would I do for the from an int array to a pointer part?
 
  • #4
Well for one thing charLoc is an array of pointers to INT and you allocate it with structs make them arrays of pointers to structs
 
  • #5
So, if I get what you are saying, would I change this:
Code:
charLoc [i] = (int *) malloc (NUM_COLS * sizeof (CharPointers));
to this:
Code:
charLoc [i] = (int *) malloc (NUM_COLS * sizeof (CharPointers *));

and if the above is correct, do you know what I would need to change to make this:
Code:
charLoc[i][j].CharPointers.pPointer = NULL;
work?
 
  • #6
Drop the type casts from malloc, you don't need it, and more like this
Code:
CharPointers ** charLoc =malloc (NUM_ROWS * sizeof (CharPointers*));

Code:
charLoc [i] = malloc (NUM_COLS * sizeof (CharPointers));

Now when you are trying to reference it you are trying to reference a data type instead of the variable name itself example

Code:
charLoc[i][j].CharPointers.pPointer = NULL;

CharPointers is a struct what you want is the variable so drop the Charpointers part and call it like this

Code:
charLoc[i][j].pPointer = NULL;
 
Last edited:
  • #7
Yes, it's working. Thanks for the help.
 

1. What is a 2D array in C and how does it differ from a regular array?

A 2D array in C is a data structure that allows for the storage of data in a two-dimensional grid format. Unlike a regular array, which is a single row or column of data, a 2D array has both rows and columns, allowing for more complex data organization.

2. What is a struct in C and how is it used in conjunction with 2D arrays?

A struct in C is a user-defined data type that allows for the grouping of different data types under one name. It is often used in conjunction with 2D arrays to create more complex data structures, where each element in the array is a struct containing multiple data points.

3. Why would I use pointers with 2D arrays and structs?

Pointers in C are used to store the memory addresses of variables, and they are frequently used with 2D arrays and structs for efficient data manipulation. Pointers allow for direct access to specific elements in a 2D array or struct, making it easier to modify or retrieve data.

4. How do I access and modify elements in a 2D array of structs?

To access and modify elements in a 2D array of structs, you will first need to declare and initialize the array. Then, you can use nested for loops to iterate through the rows and columns of the array. To access a specific element, you can use pointers to navigate to the desired location in the array and then use the dot operator to access the specific data point within the struct.

5. Are there any potential pitfalls or common errors when working with 2D arrays and structs in C?

When working with 2D arrays and structs in C, it is important to remember that each element in the array is a separate struct, so modifications made to one element will not affect the others. Additionally, care must be taken when using pointers to ensure that they are pointing to the correct memory addresses and that they are properly dereferenced to avoid segmentation faults.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
1
Views
942
  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
22
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top