Home > database >  Attempt to access elements of a 2d struct array failing
Attempt to access elements of a 2d struct array failing

Time:03-07

    typedef struct{
        unsigned long a;
        unsigned long b;
        unsigned long c;
    } mini_struct;    


    struct ministruct** build_2Dstruct(unsigned long x, unsigned long y){
        double x_squared = pow(2, x);
        struct ministruct** temp = (mini_struct**)malloc(x*sizeof(mini_struct*));
        for(int i = 0; i < x_squared; i  ){
            temp[i] = (mini_struct*)malloc(y*sizeof(mini_struct));
            for(int j = 0; j < y; j  ){
                temp[i][j].a = 0;
                etc....
            }
        }
        return temp;
    }

In the code above I am trying to create a 2D array of ministructs**, with the whole struct being made out of 2^x ministructs*, and each ministruct* has y amount of ministructs.

aka: x = 2, y = 2,

[[struct, struct], [struct, struct], [struct, struct], [struct, struct]] 

However, for some reason when I try to access the second element or index 1 of the struct inside each struct*, it says there is an error: "expression must be pointer to complete object".

I just do not understand why the code is not allowing me to access each individual element of the elements of the array?

Thanks

CodePudding user response:

You are trying to make an x by y array of structs. So:

// create array of x pointers
mini_struct **temp = malloc(x*sizeof(mini_struct*));
for (int i=0; i<x; i  ) {
    // to array of y structs
    temp[i] = malloc(y*sizeof(mini_struct));
    for (int j=0; j < y; j  ) {
        temp[i][j].a = 0;
        ... etc.

CodePudding user response:

Question is incomplete so I will be making asumptions.

You seem to be wanting to allocate a 2D array of structs and initialize all members to 0. Here is a possible solution:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

typedef struct mini_struct{
    unsigned long a;
    unsigned long b;
    unsigned long c;
} mini_struct;

struct mini_struct** build_2Dstruct(unsigned long x, unsigned long y){
    double x_squared = pow(x, 2);
    mini_struct **temp = (mini_struct **) malloc(x_squared * sizeof(mini_struct*));
    
    for(int i = 0; i < x_squared; i  ){
        temp[i] = (mini_struct *) calloc(y, sizeof(mini_struct));
    }

    return temp;
}

int main () {
    int x = 3;
    int y = 4;

    mini_struct **struct2D = build_2Dstruct(x, y);

    int x_squared = pow(x,2);
    for (int i = 0; i < x_squared;   i) {
        for (int j = 0; j < y;   j) {
            printf("Value of data stored at struct[%d][%d] is: %d\n", i, j, struct2D[i][j]);
        }
    }
    
    for (int i = 0; i < x_squared;   i) {
            free(struct2D[i]);
        }
    free(struct2D);

}

As you can see, this contains the whole program, not just the snippet you showed. In this case, a main function would have been useful so that we don't have to guess what you want to do. My solution creates the 2D array with all elements initialized to 0 (you can use calloc to do that, no need for a second for loop).

Another important point is that, because the function returns a newly heap allocated 2D array, you need to free it to avoid a memory leak (end of main function).

CodePudding user response:

You allocate x pointers to mini_struct:

mini_struct **temp = (mini_struct **) malloc(x_squared * sizeof(mini_struct*));

But then when you initialize them:

    for(int i = 0; i < x_squared; i  ){
        temp[i] = (mini_struct *) calloc(y, sizeof(mini_struct));
    }

You index temp based on upto x_squared.

Consider if x is 2. You would allocate temp to be an array of two pointers to mini_struct. But then your for loop would attempt to initialize four elements in temp.

  •  Tags:  
  • c
  • Related