sagar patel
sagar patel

Reputation: 31

How do I return "struct" data types in c user defined function?

I need to read 5 city name , whats wrong with my code, please explain I don't want to use void data type

//read the 5 city using struct
struct census
{
    char city[20];
    int popullation;
    float literacy;
};

struct census citi[5];
struct census read(struct census citi[]);

main()
{
   int i;
   citi= read(citi);
   for(i=0;i<5;i++)
   {
       printf("%s",citi[i].city);
       printf("\n");
   }
}

struct census read(struct census citi[])
{
    int i;
    for(i=0;i<5;i++)
        gets(citi[i].city);

    return(citi);
}

how to return the values using data type struct , please find error and explain me the error

Upvotes: 1

Views: 1253

Answers (3)

BLUEPIXY
BLUEPIXY

Reputation: 40145

citi= read(citi); : It is not possible to set the values to the array itself.
struct census read(struct census citi[])...return(citi); : Different type returned by the function.

There is no need to return an array function in case you want to change its contents by passing an array to a function.
You must be received by the pointer if also return an array like return(citi);.
E.g

struct census *read(struct census citi[]);
....
struct census *cities = read(citi);

If you return to one structure without return a pointer, do the following.

#include <stdio.h>

struct census {
    char city[20];
    int popullation;
    float literacy;
};

struct census read(void);

int main(void){
    struct census citi[5];
    int i;

    for(i=0; i<5; i++)
        citi[i] = read();

    for(i=0; i<5; i++)
        printf("%s\n", citi[i].city);

    return 0;
}

struct census read(void){
    struct census citi;

    scanf("%19[^\n]%*c", citi.city);

    return citi;
}

Upvotes: 0

MiCo
MiCo

Reputation: 401

Either you return a single struct

 struct census readCity(void)

and have the loop over the number of cities in the calling context, or you pass a pointer to your struct to the function:

void readCity(struct census *pCensus)

With the second version you can have the loop in the calling context

void readCity(struct census *pCensus)
{   
   fgets(pCensus->city, sizeof(pCensus->city), stdin);
}

int main(void)
{
   int i;
   for (i=0; i<5; ++i)
   {
      readCity(&citi[i]); /* pointer to single struct census */
   }
   return 0;
}

or in the function itself

void readCity(struct census *pCensus) /* pointer to an array with 5 elements */
{   
   int i;
   for (i=0; i<5; ++i)
   {
      fgets(pCensus->city, sizeof(pCensus->city), stdin);
   }
}

int main(void)
{
   readCity(citi); /* array is converted to a pointer */
   return 0;
}

Using a pointer is more flexible and has probably a better performance, because it is just an address pointing on the place, where the data should be read to.

Upvotes: 0

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

Your program as is does not require you to return anything from the read() function. Do not call your own function read() because that's the name of a standard function, so it would be better if you define it this way

void readCities(struct census *citi, size_t count)
{
    size_t index;
    for (index = 0 ; index < count ; index++)
     {
        fgets(citi[i].city, sizeof(citi[i].city), stdin);
     }
}

and the in main()

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

struct census
{
    char city[20];
    int popullation;
    float literacy;
};
void readCities(struct census *citi);

int main()
{
   size_t        index;
   struct census citi[5];

   readCities(citi, sizeof(citi) / sizeof(*citi));
   for (index = 0 ; index < 5 ; index++)
    {
       printf("%s\n", citi[index].city);
    }
   return 0;
}

the code above will initialize the struct's and as you see you don't need a global variable, don't use global variables unless you really know what you are doing, as @Weather Vane commented below, you could check the return value of fgets() and return the number of succesfuly read structs instead of not returning at all, like this

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

struct census
{
    char city[20];
    int popullation;
    float literacy;
};
size_t readCities(struct census *citi);

int main()
{
   size_t        index;
   struct census citi[5];
   size_t        count

   count = readCities(citi, sizeof(citi) / sizeof(*citi));
   for (index = 0 ; index < count ; index++)
    {
       printf("%s\n", citi[index].city);
    }
   return 0;
}

size_t readCities(struct census *citi, size_t count)
{
    size_t index;
    size_t successfulCount;

    successfulCount = 0;
    for (index = 0 ; index < count ; index++)
     {
        if (fgets(citi[i].city, sizeof(citi[i].city), stdin) != NULL)
            successfulCount += 1;
     }

    return successfulCount;
}

Upvotes: 3

Related Questions