GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Oct-2004, 18:51
Kareem1984 Kareem1984 is offline
New Member
 
Join Date: Oct 2004
Posts: 4
Kareem1984 is on a distinguished road

User defined data types


Do I create a struct oustide of my main function?

ie. like this:

CPP / C++ / C Code:
typedef struct element {
	char *data;
	struct element *next;
} LIST; //can this go outside main as well?

void main (void) {

//code
Last edited by LuciWiz : 07-Oct-2004 at 00:00. Reason: Please insert your C code between [c] [/c] tags
  #2  
Old 06-Oct-2004, 19:26
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Yes, you can do this.

Defining a variable or variable defination outside main() in program file is valid. This way the variable is available for all the functions in the source file i.e. all the function can see LIST definition. Each function can assign its own variable of the type LIST in their code. However these variables will be available only inside those function. In other words, scope of LIST data type is global to program while the locally assigned varibales (inside each function including main()) of type LIST have local scope only inside function.

Following is valid.

CPP / C++ / C Code:
typedef struct element {
char *data;
struct element *next;
} LIST; 

void main (void) {

LIST *anode;

}

void somefunction()
{
 LIST *bnode;
}


This is valid too

CPP / C++ / C Code:
typedef struct element {
char *data;
struct element *next;
} LIST; 

LIST *anode;

void main (void) {

while (anode!=NULL)
{
printf("%s",anode->data);
}

}

void somefunction()
{
 while (anode!=NULL)
{
printf("%s",anode->data); 
}

}

However this is not valid.

CPP / C++ / C Code:
typedef struct element {
char *data;
struct element *next;
} LIST; 


void main (void) {

LIST *anode;

while (anode!=NULL)
{
printf("%s",anode->data);
}

}

void somefunction()
{
 while (anode!=NULL)
{
printf("%s",anode->data);  // Not correct.anode is available only inside main()
}

}

 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
How to handle user submitted data from a form? gangster-20 Web Design Forum 10 22-Apr-2004 08:13
Automate a data change php form mjfmn MySQL / PHP Forum 4 20-Oct-2003 10:37
Script needed for letting user input a few days of data for tracking and analysis. tradertt MySQL / PHP Forum 3 06-Mar-2003 03:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.