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

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?



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

Quote:

>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?

How about:

        dlist->names[i] = (char *)malloc(100*STRLEN);

if I'm understanding what you're trying to accomplish and that is assigning
the address of allocated memory to a particular pointer in the array of
pointers known as "names" inside the structure pointed to by dlist.

Jeff S.{*filter*}son



Mon, 21 Apr 1997 00:29:25 GMT  
 problem realloc array inside array of struct
After looking at this for awhile I found a few things wrong, but the
question is still basically the same.
In case it's not obvious, I've nebeen out of C for some time now.

Erin Garlock
======================
#define STRLEN 80

struct permiss {
    char fname[STRLEN], desc[STRLEN], perms[4];
    char **names[STRLEN];   /* I assume this is a ptr to an array of strings
    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?  what I want to do is allocate a space



Mon, 21 Apr 1997 02:08:37 GMT  
 problem realloc array inside array of struct


Quote:
>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;

You must define a struct dlist somewhere - it's probably best to do it
here. The problem with a pointer is that it isn't initialised to point
to anything.

Quote:
>{

>    for(i=0; i<unum; i++)
>    {
>        dlist[i]???*names = (char *)malloc(100*STRLEN);
>               ^^^^^
>              what goes here?

This looks rather confused. The line:

Quote:
>    char *names[STRLEN];

declares the structure member names as an array of STRLEN (80) pointers
to char which doesn't look a logical use for STRLEN here. How big did
you actually want the array to be? In:

Quote:
>        dlist[i]???*names = (char *)malloc(100*STRLEN);

You're reserving 8000 characters for each string - is this what you
wanted? Lets say you wanted to declare an array of 100 pointers to string
and allocate a buffer of 80 characters for each of them you might use
something like:

#define STRLEN 80
#define NUMSTRINGS 100

struct permiss {
    char fname[STRLEN], desc[STRLEN], perms[4];
    char *names[NUMSTRINGS];
    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.names[i] = malloc(STRLEN);
   .
   .

also don't forget that if you need to represent STRLEN characters in the string
you need to malloc STRLEN+1 to hold the terminating '\0'.

What is ulist? If it is just a pointer to a string you should declare it as:

char *ulist;

If it is a pointer to an array of string pointers it would be better as

char **ulist;

As it is STRLEN seems to be inappropriate here. If it is a pointer to
literally an array of strings of 80 characters (i.e. a 2 dimensional array,
not using pointers) it should be:

char (*ulist)[STRLEN];

--
-----------------------------------------


-----------------------------------------



Wed, 23 Apr 1997 06:49:09 GMT  
 
 [ 4 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