5

I have a question about how C function returns static variable:

in data.h file:

#include <stdio.h>
#include <stdlib.h> 

typedef struct
{
   int age;
   int number;
} person;

person * getPersonInfo();

in data.c

#include "data.h"
static struct person* person_p = NULL;

person * getPersonInfo()
{
   person_p = (struct person*)malloc(10 * sizeof(struct person));
   return person_p;
}

in main.c

#include "data.h"

int main()
{
   person* pointer = getPersonInfo();
   return 0;
}

function getPersonInfo() returns a pointer which is a static pointer in data.c, is this allowed and legal? in the main.c, can the function getPersonInfo() be used like this: person* pointer = getPersonInfo();

13
  • Do not cast the return value of malloc(), static in this context is related to the scope, the sotrage is always static for global variables. Commented Jun 3, 2015 at 13:39
  • what does this mean "static in this context is related to the scope, the sotrage is always static for global variables" Commented Jun 3, 2015 at 13:42
  • Using static inside a function means that the variable has static storage class, which causes that it preserves the value accross calls to the function, static at the file scope means that you cannot externalize the variable/function to a different .c file. Commented Jun 3, 2015 at 13:44
  • static keyword effects 2 things, visibility (depending on linkage) and lifetime.. Using static for a variable declared with global scope is redundant. Commented Jun 3, 2015 at 13:45
  • 1
    function getPersonInfo() returns a pointer person_p holding value(value of return of malloc, side-effect assing to person_p). Commented Jun 3, 2015 at 13:49

3 Answers 3

5

Line

static struct person* person_p = NULL;  

declares a variable person_p which has static storage duration, file scope and internal linkage. It means that it can be referenced by name only in file data.c and internal linkage restricts its sharing to this single file.

Static storage duration means once the memory is allocated to person_p it will stay at the same storage location as long as program is running, allowing it to retain its value indefinitely. It means you can return a pointer to that location.
Therefore, your code is valid and legal.

Sign up to request clarification or add additional context in comments.

7 Comments

but the pointer is defined as static, which is visible only in data.c
Is this an answer to the post, or a comment to a specific follow-on question in the comments???
The question is about returing the pointer from a function. But the answer talks about returing a pointer to a static variable isn't it? Or am I not on track?
@Gopi; Question asked by OP: function getPersonInfo() returns a pointer which is a static pointer in data.c, is this allowed and legal?
@ratzip, that person_p has internal linkage (as a static, file-scope variable) is not relevant to whether a function may return a copy of its value. It is the variable that is static, not its value. In fact, however, it would also be fine to return a pointer to the variable (&person_p), which is as close as you can come to returning the variable itself.
|
1

person_p is a variable whose scope is just for the file data.c as it is a static variable. You are using this variable to allocate some memory on heap and return the address to main.c and there is a variable which holds this value in main.c i.e. pointer.

So here you are not returning the variable but the value the variable is holding which is totally fine because even if the variable goes out of scope the value returned(i.e. address) is still valid which is on heap.

Comments

1

It is legal, as already noted in the other answers. You could even rewrite your data.c to this:

#include "data.h"
static struct person person;

person * getPersonInfo()
{
   return &person;
}

This will return a pointer to your structure in data memory, without any need to allocate it first on the heap. The static keyword is just about the scope of your symbol person (local to data.c) but the data itself is valid global in your program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.