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

    Join Date
    Mar 2003
    Posts
    5
    Rep Power
    0

    typedef array problems


    Hi, I have a few small problems trying to use typedef. I have a typedef like this:

    struct node {

    char name[20];
    struct node *next;

    };

    typedef struct node List;

    Then I want an array of pointers to Lists. I tried this:

    typedef List *Set[997];

    But the compiler gave this warning:

    parse error before '*' token
    warning: data definition has no type or storage class

    How do I make an array full of pointers to Lists? The main thing is that I want every element to be initialized to NULL like this:

    Set thisSet = { {'\0'} };

    But that doesn't seem to work when I have tried different things. Any ideas how to do this? Thanks for any help.

    Kevin
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,640
    Rep Power
    4247
    >> typedef List *Set[997];
    >> But the compiler gave this warning:
    >> parse error before '*' token
    >> warning: data definition has no type or storage class

    Since you already have List typedef'd as struct node, you simply need to declare your variables as
    List *Set[997];
    >> How do I make an array full of pointers to Lists? The main thing is that I want every element to be initialized to NULL like this:
    Try declaring your array as:
    List *Set[997] = {0};
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: typedef array problems


    Just for further clarification:

    '\0' is the null character.

    NULL is a null pointer (which is actually just defined as 0).

    But, since chars can be typecast into bytes without a problem, '\0' will be typecast into 0. But, I would assume good coding pratice is to use NULL or 0 for a null pointer, and use the null character ('\0') for string termination.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    Also, the following will only put a NULL in the first element of the array, not each of the elements.

    List *Set[997] = {0};
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Also, the following will only put a NULL in the first element of the array, not each of the elements.

    List *Set[997] = {0};


    I'm sorry but you're misinformed. That statment does set the first element to 0, but any elements you don't provide an initial value for in an initializer list will also be initialized with 0, so a statement like this:

    double a[100][100]={0};

    will initialize every element of the 100x100 array with 0. In fact,

    double a[100][100]={};

    will initialize every element of the array with 0. And, a statement like:

    double a[100][100] = {1, 2, 3};

    will initialize the first three elements with the values shown and all the other elements will be initialized with 0.

    The same holds true for an array of pointers.
    Last edited by 7stud; March 9th, 2003 at 03:31 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    kev123,

    It sounds like you're a little confused between the keyword "typedef" and structs. typedef allows you to create a synonym for a type that already exists. For instance, you could do something like this:

    typedef int Bigones;

    That specifies Bigones as an alternative specifier for the type int, so you could then declare a variable of type int like this:

    Bigones number = 0;

    which is equivalent to:

    int number = 0;

    A struct is an entity that can contain several variables:

    struct Book
    {
    char Title[80];
    char Author[80];
    char Publisher[80];
    int Year;
    };

    Since you would declare a Book struct like this:

    Book novel;

    The type of the variable novel is Book. Just like the type of the variable number is int in the following statement:

    int number;

    You can also use the typedef keyword in conjuction with a struct just like any other type. Here's an example:

    Code:
    #include <iostream>
    using namespace std;
    
    struct lame_scientific_name_thats_also_really_long
    {
    	int weight;
    	int length;
    };
    
    int main()
    {
    	typedef lame_scientific_name_thats_also_really_long Lizard;
    	Lizard monitor = {100, 10};
    
    	cout<<monitor.weight<<endl;
    	return 0;
    }
    Last edited by 7stud; March 9th, 2003 at 01:18 AM.
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by 7stud
    I'm sorry but you're misinformed. That statment does set the first element to 0, but any elements you don't provide an initial value for in an initializer list will also be initialized with 0, so a statement like this:

    double a[100][100]={0};

    will initialize every element of the 100x100 array with 0.
    Yes, you are correct.
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,640
    Rep Power
    4247
    Originally posted by 7stud
    A struct is an entity that can contain several variables:

    struct Book
    {
    char Title[80];
    char Author[80];
    char Publisher[80];
    int Year;
    };

    Since you would declare a Book struct like this:

    Book novel;
    Small side note -- in C you have to declare novel as:
    struct Book novel;

    In C++, you can declare it as:
    struct Book novel;
    Book novel;

    Either way is valid in C++, but only the first declaration is valid in C. This is cited as one of the improvements C++ made to C (making the struct keyword optional for struct variable declarations) :). However, if you use a typedef to make an alias for struct book like so:

    typedef struct Book BOOK;
    BOOK novel;

    The above code will work for both C and C++, since BOOK is a typedef for struct Book.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    Actually, I stand partially corrected. It is supposed to initialize the rest of the item to 0, if the item is a static data item. Otherwise, the initial value is undefined.

    I have worked on systems in the past that let you tell it what value to use to initialize data. That can be handy for finding problems with data that is not explicitly initialized.
    Last edited by 3dfxMM; March 10th, 2003 at 12:12 AM.

IMN logo majestic logo threadwatch logo seochat tools logo