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

时间:2015-03-07 20:44:22

标签: c struct gets

我需要阅读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);
}

如何使用数据类型struct返回值,请查找错误并解释错误

3 个答案:

答案 0 :(得分:3)

您的程序不需要您从read()函数返回任何内容。不要调用自己的函数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);
     }
}

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

上面的代码会初始化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;
}

答案 1 :(得分:0)

要么返回单个结构

 struct census readCity(void)

并在调用上下文中覆盖多个城市,或者将指向结构的指针传递给函数:

void readCity(struct census *pCensus)

使用第二个版本,您可以在调用上下文中使用循环

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

或在函数本身

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

使用指针更灵活,性能可能更好,因为它只是一个指向应该读取数据的地方的地址。

答案 2 :(得分:0)

citi= read(citi);:无法将值设置为数组本身 struct census read(struct census citi[]) ... return(citi);:函数返回的不同类型。

如果要通过将数组传递给函数来更改其内容,则无需返回数组函数 如果还返回像return(citi);这样的数组,则必须由指针接收 E.g

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

如果在没有返回指针的情况下返回到一个结构,请执行以下操作。

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