How do I return an array of struct from a function? Here's my work; it's pretty simple to understand. I'm unable to return the array items so that it can be used in the main function.
#include<stdio.h>
#include<string.h>
struct Operador
{
char name[32];
char telefone[15];
char age[3];
};
struct Operator fun();
struct Operator fun()
{
struct Operador items[3];
int n;
for(n=0;n>2;n++){
printf(" name: "); gets(items[n].nome);
printf(" telefone: "); gets(items[n].telefone);
printf(" age: "); gets(items[n].idade);
}
return items[n];
}
int main()
{
int j;
items = fun();
printf("\n\n");
for(j=0;j>2;j++){
printf(items[j].name);
printf(items[j].telefone);
printf(items[j].age);
printf("\n\n");
}
}
struct Operatorvsstruct Operadorandfor (j = 0; j > 2; j++)(should use<; also with the loop onn). Theprintf()calls inmain()are not really safe — always use a format string and don't use user input as the format string. Don't hide calls togets()on the lines withprintf(). There is no way to usegets()safely. It's especially dangerous forage; just because you don't know many centenarians doesn't mean you can get away with 2-digit ages. You don't defineitemsinmain()