C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 7th, 2003, 06:00 PM
kev123 kev123 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 5 kev123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old March 7th, 2003, 06:46 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 15 m 18 sec
Reputation Power: 4080
>> 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};

Reply With Quote
  #3  
Old March 7th, 2003, 07:20 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #4  
Old March 8th, 2003, 10:30 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
Also, the following will only put a NULL in the first element of the array, not each of the elements.

List *Set[997] = {0};

Reply With Quote
  #5  
Old March 9th, 2003, 12:08 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 20 m 28 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.

Reply With Quote
  #6  
Old March 9th, 2003, 12:42 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 20 m 28 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.

Reply With Quote
  #7  
Old March 9th, 2003, 11:25 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #8  
Old March 9th, 2003, 01:13 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 15 m 18 sec
Reputation Power: 4080
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.

Reply With Quote
  #9  
Old March 9th, 2003, 11:56 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > typedef array problems

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap