Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c programming using a static variable and then pointing to it ? possible?

In c when creating and returning the address in a static variable inside a function would it be the same as
initializing a simple

int sNum2 = 0 ; int * temp = &sNum2;

? static will basically have it’s own allocated size in the memory right?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and i could just point to it from the staticNum function from now?

Maybe is not a good practice but is it ok to use?

 int * staticNum(){
  int static sNum=0;
  int * temp=&sNum;
  sNum++;
  return temp;
}

>Solution :

This is valid code.

A static variable, whether declared at file scope or inside of a function, has full program lifetime. That means its address will always be valid and can be safely dereferenced at any point in the program.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading