I want to know if return pointer from a C function is good/bad design? If it is a bad practice, what would be a good practice in the following example:
The question is a continued part of: c function return 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;
}
basically, the main
function in main
file needs to get the value of all the elements of array which is pointed by static pointer person_p
, if it is not a good practice, then what a good practice should be?