LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   static structs? (https://www.linuxquestions.org/questions/programming-9/static-structs-142528/)

simbo 02-05-2004 02:47 AM

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

llama_meme 02-05-2004 02:58 AM

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

simbo 02-05-2004 03:18 AM

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

llama_meme 02-05-2004 04:00 AM

Yes, that's right.

Alex


All times are GMT -5. The time now is 06:50 AM.