【问题标题】:Pointer to a type-defined structure指向类型定义结构的指针
【发布时间】:2021-10-16 13:32:48
【问题描述】:

好的,所以我有一个之前编写的以下代码。这是针对我正在做的一项作业,我无法更改这些代码行:

typedef struct
{
    int number;
    float average;

} B_PLAYER;


typedef struct
{
    char n[20];
    B_PLAYER k[12];

} TEAM;

还有两个声明的函数,如下所示:

int result (TEAM t1, TEAM t2); 

↑暂时排除这个函数,这会计算匹配结果

TEAM* createTeam();

我必须编写的代码应该模拟一场篮球比赛(总共 32 支球队的多场比赛)。因为作业太长(而且有点复杂),所以我会尽量简短。所以上面代码的第一部分定义了名为 B_PLAYER 和 TEAM 的类型,我理解。但我的问题是,为什么类型名称 TEAM 用作函数返回类型,指向该类型名称的指针是什么意思(do):

TEAM* createTeam(); 这个

请记住,此函数需要创建篮球队的地址并返回相同的地址。它必须从 main() 函数(标准输入)读取值。所有 12 个位置和所有套件编号都必须填写。

我很高兴收到一些反馈。如果您需要更多信息,我会写下整个作业。 :)

【问题讨论】:

  • why is the type name TEAM used as a function name 它不用作函数名 - 它是函数的返回类型。你的函数名称是createTeam,它返回类型TEAM*。您应该搜索有关 C 中指针的教程(提示 - 您的函数应该在某个时候调用 malloc 来为返回的值分配内存 - 您应该在程序的另一个位置调用 free
  • 这听起来像是应该在你的课程材料中解释的东西。你问老师了吗?
  • TEAM* createTeam(); 简单地表示createTeam() 是一个函数,它返回一个指向TEAM 类型对象的指针
  • 我认为您的问题中有错字。 PLAYER k[12]; 应该是 B_PLAYER k[12];?
  • @IanAbbot 是的,你是对的。我的问题中有一个错字。谢谢。

标签: c function pointers structure


【解决方案1】:

但我的问题是,为什么类型名称 TEAM 用作函数返回类型,指向该类型名称的指针是什么意思(do):

TEAM* createTeam();

你读错了。 TEAM 用作函数返回类型!

TEAM* 被解读为“一件事”——你不能像 TEAM 有一个含义和 * 有另一个含义那样分成两个。它必须读作TEAM*,这意味着指向 TEAM 对象的指针

所以TEAM* createTeam(); 意味着您必须返回指向 TEAM 对象的指针的值。简而言之,我们只是说:“返回指向 TEAM 对象的指针”只是因为它在 C 中隐含函数总是返回值。

那么如何获取 TEAM 对象并在函数中返回指向它的指针呢?

答案是malloc(和朋友们)。喜欢:

TEAM* createTeam()
{
    TEAM* team_ptr;                       // Create a pointer to a TEAM object
    team_ptr = malloc(sizeof *team_ptr);  // Allocate memory for a TEAM object and
                                          // assign the address of that memory
                                          // to the pointer
    if (team_ptr == NULL) exit(1);        // Error checking
    return team_ptr;                      // Return (the value of) the pointer to the TEAM object
}

在例如main你可以这样做:

TEAM* teamA = createTeam();
TEAM* teamB = createTeam();

现在您有 2 个指向不同 TEAM 对象的指针。

但是等等……它们没有价值。不,您需要添加一些代码来读取要分配给 TEAM 对象的值。可能是这样的:

TEAM* createTeam()
{
    TEAM* team_ptr;
    team_ptr = malloc(sizeof *team_ptr);
    if (team_ptr == NULL) exit(1);

    // Read team name
    fgets(team_ptr->n, sizeof team_ptr->n, stdin);
    size_t len = strlen(team_ptr->n);
    if (len > 0) team_ptr->n[len-1] = '\0';  // remove newline if present

    // Read the players
    for (int i = 0; i < 12; ++i)
    {
        team_ptr->k[i].number = 42;    // Add your own code to get it from stdin
        team_ptr->k[i].average = 42.0; // Add your own code to get it from stdin
    }
    return team_ptr;
}

提示:fgets 和 sscanf 可能对“缺失”代码有用。

所以现在你可以这样做,例如main

TEAM* teamA = createTeam();
TEAM* teamB = createTeam();
printf("Next match will be %s playing %s\n", teamA->n, teamB->n);
printf("Player number %d at team %s has average %f\n", teamA->k[0].number, teamA->n, teamA->k[0].average);

一旦你完成了你所做的团队:

free(teamA);
free(teamB);

【讨论】:

  • 先生,您帮了我很大的忙,谢谢!你介意我把作业发给你吗?您的 anwser 提供了很大帮助,如果您确切知道在此特定任务中需要做什么,我认为我们会在同一页面上。 :)
【解决方案2】:

TEAM* createTeam(); 声明了一个函数,该函数返回一个指向在别处分配的TEAM 结构变量的指针。意思是里面的某个地方可能有这样的代码:

TEAM* t = malloc(sizeof *t);
...
return t;

作为旁注,空括号在 C 中是过时的样式。你应该写 TEAM* createTeam(void)

【讨论】:

  • 非常感谢您的回答!我知道空括号已经过时了,但是我们需要坚持这种类型的声明,因为它是固定的,我们不能改变它。我可以发布整个作业,也许这样更容易理解需要做什么。感谢您的反馈。 :)
猜你喜欢
  • 2022-11-23
  • 2010-10-05
  • 2023-03-18
  • 2018-10-20
  • 2019-03-18
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多