【问题标题】:How to return a pointer from a function in C?如何从 C 中的函数返回指针?
【发布时间】:2016-04-26 11:22:43
【问题描述】:

我有一个名为playerInformation 的结构,我想从我的C 程序中的一个函数返回,下面的函数是我编写的那个。

它找到了正确的结构,我可以使用printf 打印函数内的详细信息。但是,我似乎无法返回一个指针,以便我可以在主函数中打印信息。

使用此代码,我会收到以下警告:

MainTest.c: In function ‘main’:
MainTest.c:34: warning: assignment makes pointer from integer without a cast

MainTest.c(第 33 和 34 行)

struct playerInformation *test;
test = findPlayerInformation(head, 2);

结构函数.c

struct playerInformation *findPlayerInformation(struct playerInformation *head, int playerIndex) {
    struct playerInformation *ptr;
        for(ptr = head; ptr != NULL; ptr = ptr->next) {
            if(ptr->playerIndex == playerIndex) {
                return ptr;
            }
        }
    return NULL;
}

【问题讨论】:

  • findPlayerInformation 声明的(放在一个头文件中,包含在main 定义之前的maintest.c 中)?
  • 使用前先放原型。
  • 实际上它“只是”一个警告.. findPlayerInformation 位于您的主函数下方还是您之前定义了函数原型?
  • purplepsycho 是对的,来自 java 背景我完全省略了将新函数添加到我的头文件中,为愚蠢的问题道歉!
  • 正确配置您的编译器。低于版本 5 的 GCC 默认设置为错误的编译器 (gnu90)。而是使用gcc std=c11 -pedantic-errors,它会告诉你到底是什么问题(缺少函数原型)。

标签: c function pointers struct


【解决方案1】:

Put prototype before use.BLUEPIXY

曾几何时,SO 文档中的“从另一个 C 文件调用函数”主题涵盖了该问题。

在这种情况下,您需要一个定义 struct playerInformation 类型的标头:

playerinfo.h

#ifndef PLAYERINFO_H_INCLUDED
#define PLAYERINFO_H_INCLUDED

struct playerInformation
{
    ...
};

extern struct playerInformation *findPlayerInformation(struct playerInformation *head, int playerIndex);

#endif

structFunctions.c 中的代码应包含标题:

#include "playerinfo.h"

...

struct playerInformation *findPlayerInformation(struct playerInformation *head, int playerIndex) {
    struct playerInformation *ptr;
        for(ptr = head; ptr != NULL; ptr = ptr->next) {
            if(ptr->playerIndex == playerIndex) {
                return ptr;
            }
        }
    return NULL;
}

主程序也会包含标题:

MainTest.c

#include "playerinfo.h"

...

int main(void)
{
    struct playerInformation *head = ...;
    ...
    struct playerInformation *test;
    test = findPlayerInformation(head, 2);
    ...
    return 0;
}

【讨论】:

    【解决方案2】:

    您已将struct playerInformation *ptr; 声明为findPlayerInformation() 函数内的局部变量...因此上述指针的范围将仅在findPlayerInformation() 函数内可用。

    if(ptr->playerIndex == playerIndex) 
         return ptr;
    

    所以在这个语句之后,控制将转到 main 函数。由于您在 findPlayerInformation() 函数中将 ptr 声明为局部变量,因此您将无法在 main 函数中获得预期的 ptr..

    解决办法:

    如果您想避免这个问题,请将 ptr 声明为静态变量,如下所示

    static struct playerInformation *ptr; 
    

    用于在整个文件中保持变量范围的静态关键字...

    【讨论】:

    • 我认为您因为更改作为参数传递的指针而感到困惑。发布的代码很好,不需要静态。此外,这不能解释给定的编译器警告。
    • 对不起。刚才我看到函数调用和函数定义在不同的源文件中..因此您可以将结构指针的地址作为另一个参数传递给 findPlayerInformation() 函数&在该函数中,您可以填写此地址..跨度>
    • " 这样你就可以将结构指针的地址作为另一个参数传递" 这就是他正在做的事情。函数调用和函数定义放在哪个文件中并不重要。
    • 实际上在我之前的评论中,我已经提到使用“静态变量声明”..所以,如果你想使用静态声明,那么函数调用和定义应该放在同一个文件中..为此只有我提到了源文件..&“这就是他正在做的事情”,我无法找到,他将目的地的地址传递给 findPlayerInformation()????? findPlayerInformation(head, 2) 他只是传递链表的头部和一个标准来找到必要的结构.....
    • 我无法找到他传递目的地地址的位置 - 他不需要传递目的地地址,因为他按值返回指针,并且由于指向的非本地对象的生命周期不会在函数返回时结束,因此这是有效的。
    猜你喜欢
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 2010-11-03
    • 2015-08-05
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多