LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A question about assigning structs to 2d struct array... (https://www.linuxquestions.org/questions/programming-9/a-question-about-assigning-structs-to-2d-struct-array-4175520771/)

trist007 10-01-2014 02:56 PM

A question about assigning structs to 2d struct array...
 
Code:

struct Init {
        struct Domains *Dptr;
};

typedef struct Init Init;

struct Domains {
    u_int count;
    struct Domain **dptr;
};

typedef struct Domains Domains;

struct Domain {
        u_int GET, POST;
        u_int num_requests;
        char name[32];
        struct Request *requests[1];
};

typedef struct Domain Domain;

void AddDomain(Init *init, char *host)
{
    Domain *domain = calloc(1, sizeof(struct Domain));
    init->Dptr->dptr = domain;
    strncpy(init->Dptr->dptr[0]->name, host, 32);
}

Init *Initialize()
{
    Init *init = calloc(1, sizeof(Init));
    Domains *domains = calloc(1, sizeof(struct Domain));
    init->Dptr = domains;
    init->Dptr->count = 0;

    return init;
}

In the AddDomain(), how can I assign the dptr in struct Domains with the new struct Domain pointer?

This is the part is where I need help.
Code:

init->Dptr->dptr = domain;
strncpy(init->Dptr->dptr[0]->name, host, 32);

Basically when I add a new domain I want to dynamically grow the struct Domain dtpr, which is in struct Domains, array by 1.

trist007 10-01-2014 06:20 PM

Ah nevermind, I need an array of struct domain pointers

Code:

struct Domains {
    u_int count;
    struct Domain *dptr[1];
};


trist007 10-09-2014 11:48 AM

I need your help with syntax and maybe logic.

My struct looks like this
Code:

struct Domains {
    u_int count;
    struct Domain *dptr[];

};

I want the *dptr[] to be an array holding pointers to struct Domain. That way in my program as I see a new domain I can dynamically add an element to the array *dptr[]. The element will simply be a struct pointer pointing to a Domain struct.

So I am thinking that I would do a malloc for the struct pointer to increase the *dptr[] array by 1 and another malloc for the actual Domain struct. Is that correct?

My pseudo ish code so far.
Code:

Domain *domain = calloc(1, sizeof(struct Domain *));  // allocate memory for struct pointer
init->Dptr->dptr[count] = &domain;                    // How do I put that pointer into init->Dtpr->dtpr[count]?

Domain *domain = calloc(1, sizeof(struct Domain));    // allocate memory for an actual Domain struct
init->Dptr->dptr[count] = domain;                    // I think this part is correct

Here is the Domain struct.
Code:

struct Domain {
        u_int GET, POST;
        u_int num_requests;
        u_int total_requests;
        char name[32];
        struct Request *requests[];
     
};

The same would apply to the *requests[] array in the Domain struct.

ntubski 10-09-2014 12:52 PM

Quote:

Originally Posted by trist007 (Post 5251425)
I would do a malloc for the struct pointer to increase the *dptr[] array by 1 ... Is that correct?

No, a malloc of size 1 element only gives you space for another element that isn't adjacent to your existing array, so it's useless for growing your array. You need to allocate a new array which is one element larger, copy all the old elements, and then copy the new one into the last place. The realloc() function can do the first part for you.

trist007 10-09-2014 01:30 PM

Awesome! Thank you for pointing me in the right direction.


All times are GMT -5. The time now is 10:23 AM.