LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2004, 03:47 AM   #1
simbo
LQ Newbie
 
Registered: Apr 2003
Posts: 17

Rep: Reputation: 0
static structs?


Hi all,
I am quite confused over a bit of code in C. Now here's my problem there is a function below, and this function gets called by a bunch of other functions. (Note: this is not in main()).

struct eToken * get_token_by_lun(DWORD Lun){
static struct eToken eToken;

if(Lun==0)
return &eToken;

return NULL;
}
now what happens everytime this function gets called, does it create a new eToken every single time?, ot does it create it first time, and never again?I'm confused by the static part, 'cause i thought static meant it's always there.
Thanks in advance,

Sim
 
Old 02-05-2004, 03:58 AM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
The function will return a pointer to the same eToken every time it's called -- it will behave exactly as if the eToken variable was a global variable. If you want to return a new eToken every time, you'll have to use malloc() to allocate memory dynamically.

Alex
 
Old 02-05-2004, 04:18 AM   #3
simbo
LQ Newbie
 
Registered: Apr 2003
Posts: 17

Original Poster
Rep: Reputation: 0
Talking Cheers

oh ok, so it will be the same even if the calling functions modify that struct, correct me if i'm wrong. Thanks a lot for your help.

Sim
 
Old 02-05-2004, 05:00 AM   #4
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Yes, that's right.

Alex
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy structs in c alaios Programming 10 09-10-2005 03:31 PM
calling global structs LuderForChrist Programming 8 08-11-2004 01:42 AM
pointers to structs in C spuzzzzzzz Programming 5 06-03-2004 06:41 PM
Self referential structs in C? MadCactus Programming 14 01-28-2004 06:29 PM
structs and allocation of mem h/w Programming 4 12-22-2003 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration