LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2014, 02:56 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
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.

Last edited by trist007; 10-01-2014 at 03:02 PM.
 
Old 10-01-2014, 06:20 PM   #2
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Ah nevermind, I need an array of struct domain pointers

Code:
struct Domains {
    u_int count;
    struct Domain *dptr[1];
};
 
Old 10-09-2014, 11:48 AM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
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.
 
Old 10-09-2014, 12:52 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by trist007 View Post
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.
 
Old 10-09-2014, 01:30 PM   #5
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Awesome! Thank you for pointing me in the right direction.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Array of pointers to structs grob115 Programming 7 11-20-2011 10:49 AM
copying an array of structs to new variable utdrmac Programming 7 06-27-2011 02:16 PM
c: iterate an array of structs kpachopoulos Programming 2 08-15-2006 11:06 AM
Array structs - passed to function cdog Programming 4 02-02-2006 03:07 PM
confusing array of structs kpachopoulos Programming 1 11-23-2005 02:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration