简体   繁体   中英

pointer to pointer in struct. How to allocate memory in function

I have a struct like this

    struct abc{
      struct xyz **x;
    }

And create pointer to abc in main

     struct abc *o=(struct abc *)malloc(sizeof (struct abc));

and I am passing it to function hello . want to pass o->x to a function hello(struct xyz * xyz_obj)

in function hello I want to allocate element with malloc call so I created function like this

        void hello(struct xyz **xyz_obj)
           {
               *xyz_obj=(struct xyz *)malloc(sizeof(struct xyz));}
                

but the problem is how to call hello so one element is added to the array.

I tried this function call like following to allocated o->x so in main I could use o->xyz+some_number but these tries not working

         hello(o->xyz+1); // NOT WORKING
         hello(*o->xyz+1) //NOT working
         hello((o)->xyz+1) //NOT working
         etc.

How I will allocate ox in hello and use it in main

please add some explanation and way to accomplish this

struct xyz { /*something here*/ };

struct abc {
    struct xyz **x;
}

void hello(struct xyz **xyz_obj)
{
    // allocate one object of type struct xyz
    *xyz_obj = malloc(sizeof(struct xyz));
}

int main()
{
    // allocate an object of type struct abc
    struct abc *o = malloc(struct abc);
    // allocate storage for N pointers to struct xyz
    o->x = malloc(sizeof(struct xyz*) * N);

    hello(o->x    ); // first
    hello(o->x + 1); // second
    hello(o->x + 2); // third
    //... N times possible

    // access of object (of type struct xyz) at index n
    // o->x[n]->/*some field id*/;

    return 0;
}

You can't allocate o->x in the function hello (well you can allocate memory, but not assign it to o->x ). For that, you have to refactor (that means, prototype of hello has to be re-declared and with that the calls to hello ).

void hello(struct xyz ***xyz_obj)
{
    if (*xyz_obj == NULL)
        *xyz_obj = malloc(sizeof(struct xyz**) * N);

    // allocate one object of type struct xyz
    **xyz_obj = malloc(sizeof(struct xyz));
}

int main()
{
    //... same as before, but now we do not allocate any storage for o->x
    o->x = NULL;

    hello(&o->x); //since o->x is NULL, hello will allocate storage for o->x
    // hello(&(o->x + n));
    
    return 0;
}

I find the second approach ugly as hell. What is the benefit of it? Just to allocate any storage if it was not allocated before.

Based on your questions in the comment section:

struct xyz {
    char var;
};

struct abc {
    struct xyz **x;
};

int main() {

    // assume 'o' is initialized and 'o->x' is not NULL
    struct abc *o;

    // access 'var' of 'struct xyz' at position (index) 'n'
    o->x[n]->var = 'c';

    // or
    struct xyz *x = o->x[n];
    x->var = 'c';

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM