简体   繁体   English

如何在c用户定义的函数中返回“struct”数据类型?

[英]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 我需要阅读5个城市名称,我的代码有什么问题,请解释我不想使用void数据类型

//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 如何使用数据类型struct返回值,请查找错误并解释错误

Your program as is does not require you to return anything from the read() function. 您的程序不需要您从read()函数返回任何内容。 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 不要调用你自己的函数read()因为这是标准函数的名称,所以如果用这种方式定义它会更好

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() 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 上面的代码将初始化struct ,并且如你所见,你不需要全局变量,除非你真的知道你在做什么,否则不要使用全局变量,正如@ Weather Vane在下面评论的那样,你可以检查返回值fgets()并返回成功读取结构的数量而不是根本不返回,就像这样

#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;
}

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. 使用指针更灵活,可能具有更好的性能,因为它只是指向应该读取数据的地方的地址。

citi= read(citi); : It is not possible to set the values to the array itself. :无法将值设置为数组本身。
struct census read(struct census citi[]) ... return(citi); 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); 如果还返回一个像return(citi);这样的数组,你必须被指针接收return(citi); .
Eg 例如

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM