problem realloc array inside array of struct 
Author Message
 problem realloc array inside array of struct


Newsgroups: comp.lang.c
Subject: problem realloc array inside array of struct
Organization: The Ohio State University

I am trying to dynamically allocate an array of string inside a
pointer to struc.   My question is how do I reference this array for
dynamic allocation?  I have included a snippet of code to clarify
things.

Any help will gratefully appreciated

Erin Garlock

===================================================================
#define STRLEN 80

struct permiss {
    char fname[STRLEN], desc[STRLEN], perms[4];
    char *names[STRLEN];
    int numnames;
    struct stat *fstatus;

Quote:
};

void D_makedirs(ulist, unum, dlist)
  char *ulist[STRLEN];
  int unum;
  struct permiss *dlist;
{

    for(i=0; i<unum; i++)
    {
        dlist[i]???*names = (char *)malloc(100*STRLEN);
               ^^^^^
              what goes here?
--
|Fidonet:  Erin J Garlock 17:514/115

|
| Standard disclaimer: The views of this user are strictly his own.



Sun, 20 Apr 1997 23:40:03 GMT  
 problem realloc array inside array of struct

Quote:

>Newsgroups: comp.lang.c
>Subject: problem realloc array inside array of struct
>Organization: The Ohio State University
>I am trying to dynamically allocate an array of string inside a
>pointer to struc.   My question is how do I reference this array for
>dynamic allocation?  I have included a snippet of code to clarify
>things.
>Any help will gratefully appreciated
>Erin Garlock

>===================================================================
>#define STRLEN 80
>struct permiss {
>    char fname[STRLEN], desc[STRLEN], perms[4];
>    char *names[STRLEN];
>    int numnames;
>    struct stat *fstatus;
>};
>void D_makedirs(ulist, unum, dlist)
>  char *ulist[STRLEN];
>  int unum;
>  struct permiss *dlist;
>{
>    for(i=0; i<unum; i++)
>    {
>        dlist[i]???*names = (char *)malloc(100*STRLEN);
>               ^^^^^
>              what goes here?
>--
>|Fidonet:  Erin J Garlock 17:514/115

>|
>| Standard disclaimer: The views of this user are strictly his own.

I'm not perfectly shure, but try the following:

In your "struct permiss", you have written
    char *names[STRLEN];
i.e. names denotes an array of STRLEN pointers to character.
Probably a pointer to an array of STRLEN characters is what you want:
    char (* names)[STRLEN];

#define STRLEN 80

struct permiss {
    char fname[STRLEN], desc[STRLEN], perms[4];
    char (* names)[STRLEN];
    int numnames;
    struct stat *fstatus;

Quote:
};

void D_makedirs(ulist, unum, dlist)
    char *ulist[STRLEN];
    /*
        perhaps here: char (*ulist)[STRLEN]
        rather than char *ulist[STRLEN] ?
    */
    int unum;
    struct permiss *dlist;
{

    for(i=0; i<unum; i++)
    {
        dlist[i].names =
            (char (* )[STRLEN])
                malloc(100 * sizeof *dlist[i].names);
        /*
            allocate an array of 100 elements, each being an
            array of STRLEN characters
        */



Wed, 18 Jun 1997 11:40:05 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. problem realloc array inside array of struct

2. Problem with struct/array inside a struct using fread/fwrite

3. Structure, where is one array inside the struct...

4. Array inside of a struct gets allocated statically?

5. Timers - Inside and array of structs

6. float array inside struct

7. Accessing arrays of char pointers inside of structs.

8. Problem with realloc on a 2D-array

9. array of pointers to array of structs

10. qsorting & managing struct arrays, pointer arrays

11. Referencing an array of structs in an array of stracts

12. struct.array works - struct->pointer not

 

 
Powered by phpBB® Forum Software