Thread: Accessing structures value within structurepointer in 2D array

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    21

    Accessing structures value within structurepointer in 2D array

    Hello!

    I had no idea how to express myself in the title, but this is what I want to achieve:

    Code:
    typedef struct structureOne{
        struct structureTwo* aStructure;
    }structureOne;
    
    
    typedef struct structureTwo{
        char value;
    }structureTwo;
    
    
    main(){
        structureOne** 2Darray;
        //Allocate 2Darray of type structureOne
        //and give the structureTwo variable value
        //a value. Now print that value.
    
    
        //Not working
        printf("%c", 2Darray[x][y]->aStructure.value);
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUMROWS 5
    #define NUMCOLS 10
    
    typedef struct Data {
        char value;
    } Data;
    
    typedef struct Element {
        Data *data;
    } Element;
    
    int main(){
        Element **grid;
    
        // Allocate space for row pointers.
        grid = malloc(NUMROWS * sizeof(Element*));
        // Allocate space for all grid elements.
        grid[0] = malloc(NUMROWS * NUMCOLS * sizeof(Element));
        // Assign row pointers to element rows.
        for (int row = 1; row < NUMROWS; row++)
            grid[row] = grid[row-1] + NUMCOLS;
    
        // Allocate space for data.
        Data *data = malloc(NUMROWS * NUMCOLS * sizeof(Data));
        // Assign data to data members.
        for (int row = 0; row < NUMROWS; row++)
            for (int col = 0; col < NUMCOLS; col++)
                grid[row][col].data = data + (row * NUMCOLS + col);
    
        // Fill grid data values.
        for (int row = 0; row < NUMROWS; row++)
            for (int col = 0; col < NUMCOLS; col++)
                grid[row][col].data->value = 'A' + row + col;
    
        // Print values. 
        for (int row = 0; row < NUMROWS; row++) {
            for (int col = 0; col < NUMCOLS; col++)
                printf("%c ", grid[row][col].data->value);
            putchar('\n');
        }
    
        // Free all allcoated memory.
        free(grid[0]);
        free(grid);
        free(data);
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    21
    Wow, that was an interesting way of creating and handling a 2D array I have never thought about it in this way. It surely seems more comfortable to maintain. Thanks! Any idea why my way didn't work?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > Any idea why my way didn't work?

    In your code, 2Darray is not inherently an array. You neglected to fill out the comments that essentially say "allocate memory" and "populate values".
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Kirderf View Post
    Any idea why my way didn't work?
    You gave only the sketchiest sketch of your program (complete with illegal variable name "2DArray"), however when accessing the data you used an arrow (->) where you should have used a dot (.) and a dot where you should have used an arrow. It should be:
    Code:
    printf("%c", TwoDarray[x][y].aStructure->value);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  2. accessing an array of structures
    By creative in forum C Programming
    Replies: 4
    Last Post: 12-27-2007, 12:52 PM
  3. Help on accessing structures using 'gets'
    By phil1p in forum C Programming
    Replies: 11
    Last Post: 10-27-2005, 04:56 PM
  4. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  5. Accessing members of structures
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 02-03-2002, 12:10 PM

Tags for this Thread