|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
>> 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}; |
|
#3
|
||||
|
||||
|
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.
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#4
|
|||
|
|||
|
Also, the following will only put a NULL in the first element of the array, not each of the elements.
List *Set[997] = {0}; |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
||||
|
||||
|
Quote:
|
|
#8
|
||||
|
||||
|
Quote:
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. |
|
#9
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > typedef array problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|