Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
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?

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;
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

Yes, it's ok to use. A pointer to a static variable, is just like any other pointer. Just pointing to the place in the data segment where the static variable is stored.

over 4 years ago · Santiago Trujillo Report

0

The returned pointer from the function

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

will be valid because the static variable sNum will be alive after exiting the function.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!