The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
typedef array problems
Discuss typedef array problems in the C Programming forum on Dev Shed. typedef array problems C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 7th, 2003, 06:00 PM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation 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
|

March 7th, 2003, 06:46 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
>> 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};
|

March 7th, 2003, 07:20 PM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
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.
|

March 8th, 2003, 10:30 PM
|
|
|
|
Also, the following will only put a NULL in the first element of the array, not each of the elements.
List *Set[997] = {0};
|

March 9th, 2003, 12:08 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
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.
|

March 9th, 2003, 12:42 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
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.
|

March 9th, 2003, 11:25 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
Quote: 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.
|

March 9th, 2003, 01:13 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Quote: 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.
|

March 9th, 2003, 11:56 PM
|
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|