Typedef structs within typedef structs... 
Author Message
 Typedef structs within typedef structs...

Hi,

I wish to set a variable (var1 & var2) in a typedef struct declared
within another
typdef struct and was wondering how to do this. I have included an piece
of code below with something simular to what I am trying to achive.

If anyone could tell me how to do this it would be much appreciated.

Regards,
Mike

//1.h

typedef struct MYSTRUCT{
        int var1 ;
        int var2 ;

Quote:
}mystruct;

typedef struct STRUCT1{
        mystruct *mystructPtr[5];
        int numberOfMyStructs;

Quote:
}struct1;

================================================================
//1.c

main() {
   struct1 newStruct1[4];

   newStruct1[1].numberOfMyStructs = 5;

Quote:
}

--



Sun, 05 Oct 2003 02:48:22 GMT  
 Typedef structs within typedef structs...

Quote:

> I wish to set a variable (var1 & var2) in a typedef struct declared
> within another typdef struct and was wondering how to do this.
> //1.h
> typedef struct MYSTRUCT{
>         int var1 ;
>         int var2 ;
> }mystruct;
> typedef struct STRUCT1{
>         mystruct *mystructPtr[5];
>         int numberOfMyStructs;
> }struct1;

You don't need the structure tags.
Note that mystructPtr is an array of 5 pointers to mystruct objects;
allocating a struct1 doesn't automatically allocate any of the
mystruct objects nor initialize values for the mystructPtr array.
The mystruct objects can be allocated with malloc and the pointers
returned by malloc (if non-null) should be stored in the appropriate
elements of mystructPtr.

Quote:
> ================================================================
> //1.c
> main() {
>    struct1 newStruct1[4];
>    newStruct1[1].numberOfMyStructs = 5;
> }

Saying there are 5 mystructs doesn't make it so..

Once you have properly allocated the mystructs, accessing the
members is trivial:
        newStruct1[i].mystructPtr[j]->var1 = 42;

It should be pointed out that these data structures are weird and
have horribly uniformative names.
--



Sun, 05 Oct 2003 22:38:35 GMT  
 Typedef structs within typedef structs...

Quote:

> typedef struct STRUCT1{
>    mystruct *mystructPtr[5];
>    int numberOfMyStructs;
> }struct1;

That's five separate pointers.  If that's really what you want,
you could do something like:

  /* allocate memory and store its address in the first pointer */
  newStruct1[1].mystructPtr[0] = xmalloc(sizeof (mystruct));
  /* modify the object via the pointer */
  newStruct1[1].mystructPtr[0]->var1 = 1;
  newStruct1[1].mystructPtr[0]->var2 = 2;

where xmalloc is a function which calls malloc and aborts the
program if it runs out of memory.  (There is one below.)

As struct STRUCT1 contains a numberOfMyStructs member, it would
be more common to make mystructPtr a single pointer (not an array
of pointers) pointing to the first element of an array of
mystruct objects.  Also, array indices start from 0, not 1:

  #include <stdio.h>
  #include <stdlib.h>

  typedef struct MYSTRUCT {
    int var1;
    int var2;
  } mystruct;

  typedef struct STRUCT1 {
    mystruct *mystructPtr;
    int numberOfMyStructs;  /* or size_t, perhaps */
  } struct1;

  void *xmalloc(size_t size)
  {
    void *p = malloc(size);
    if (p == NULL)
      abort();  /* should print an error message first */
    return p;
  }

  int main(void)
  {
    struct1 newStruct1[4];
    int i, j;

    /* allocate memory */
    for (i = 0; i < 4; ++i) {
      newStruct1[i].numberOfMyStructs = 10 - i; /* for example */
      newStruct1[i].mystructPtr = xmalloc(newStruct1[i].numberOfMyStructs
                                          * sizeof (mystruct));
    }

    /* fill with values */
    for (i = 0; i < 4; ++i) {
      for (j = 0; j < newStruct1[i].numberOfMyStructs; ++j) {
        newStruct1[i].mystructPtr[j].var1 = 1000 + (i*j);
        newStruct1[i].mystructPtr[j].var2 = 2000 + (i^j);
      }
    }

    /* print them out */
    for (i = 0; i < 4; ++i) {
      for (j = 0; j < newStruct1[i].numberOfMyStructs; ++j) {
        printf("%d,%d: %d,%d\n", i, j,
               newStruct1[i].mystructPtr[j].var1,
               newStruct1[i].mystructPtr[j].var2);
      }
    }

    /* clean up -- not necessary on modern operating systems, but good
       for memory leak detection */
    for (i = 0; i < 4; ++i)
      free(newStruct1[i].mystructPtr);

    return 0;
  }

The subject of this thread looks like you think using a typedef
somehow affects the way you access nested structures or pointers.
It doesn't: if you change each occurrence of "mystruct" to
"struct MYSTRUCT" and "struct1" to "struct STRUCT1" in the
program above, you can eliminate the typedefs.

Some comments on identifier names.  You have written the struct
tags (struct STRUCT1) in upper case and typedefs in lower case;
the usual practice is just the opposite.  Typedefs and macros are
normally written in upper case to avoid using the same name for a
variable (or function); this is not necessary for "struct STRUCT1"
as the "struct" keyword already indicates it isn't a variable.

You should also choose a convention for relating the count and
pointer members to each other.  The "mystructPtr" /
"numberOfMyStructs" scheme in your example is too verbose to my
taste, and the capitalization seems random.  I've been using
names like this:

  struct omelet {
    struct egg *eggs;
    size_t neggs;
  };

i.e., the name of the count begins with "n" as in "number".  I'm
sure there are other reasonable naming conventions; whatever you
choose, please be consistent.
--



Sun, 05 Oct 2003 22:38:47 GMT  
 Typedef structs within typedef structs...


Quote:

>> I wish to set a variable (var1 & var2) in a typedef struct declared
>> within another typdef struct and was wondering how to do this.

>> //1.h
>> typedef struct MYSTRUCT{
>>         int var1 ;
>>         int var2 ;
>> }mystruct;
>> typedef struct STRUCT1{
>>         mystruct *mystructPtr[5];
>>         int numberOfMyStructs;
>> }struct1;

>You don't need the structure tags.

But they don't hurt, do they?  And without tags, it's... somewhat
difficult... to have opaque types.

Regards,

Icarus
--
The world will little note nor long remember what we say here
                           -A. Lincoln, Gettysberg Address


--



Mon, 06 Oct 2003 03:42:34 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Typedef structs within typedef structs...

2. struct vs. typedef struct

3. What the difference between "typedef struct" and "typedef struct tag_Struct"?

4. typedef struct tagNAME {} NAME; vs. typedef struct NAME {};

5. typedef struct not working under .NET C/C++ compiler

6. typedef declaration of struct

7. typedef struct ...

8. typedef struct on several NAMES

9. typedef struct

10. interpreting "typedef struct x* y"

11. typedef struct convention

12. Question about typedef struct

 

 
Powered by phpBB® Forum Software