#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    4
    Rep Power
    0

    Question double pointer to a structure within a structure


    i've been latly working on some project (neverminde the details), and i got stuck in this part....

    there are two structures involved in this problem:

    struct folder {
    char name[20];
    struct file **files;
    struct folder *subfolder;
    };

    struct file {
    char name[20];
    char *info;
    };

    now the problem comes when i'm trying to do this:

    int main () {

    struct folder home =
    {"home", (struct file *) calloc(2, sizeof(struct file)), NULL};

    home.files[0] = {"test.txt" , NULL}; /* problem */
    }

    EOF

    so the problem comes is the line i have marked with a comment. anyways, i'm trying to insert some values into one of the dynamicly allocated structures. but the compile says:

    prase error before '{'

    what have i done wrong? bah i guess i'll find my own mistake before i get an answer :\

    BTW, this program runs under linux. gcc compiler.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    First, I'm very leery of that declaration for struct folder home. I don't believe that you can call a function within an initialization list, but rather that the list's values must be known at compile time.

    But the reason you're getting a parse error on the next line is because that's an assignment statement you're trying to use an initialization list in. Initialization lists belong in declarations.

    Break your assignment statement up into a separate "assignment" (ie, including function calls like strcpy) for each field; eg:
    Code:
    strcpy(home.files[0].name,"test.txt");
    home.files[0].info = NULL;
    Last edited by dwise1_aol; April 14th, 2003 at 02:38 PM.

IMN logo majestic logo threadwatch logo seochat tools logo