2 c源文件之间的函数指针操作导致编译时错误

时间:2014-10-16 21:37:02

标签: c++ c function pointers header-files

我正在开发一个小项目来帮助我更好地理解c指针/指针函数。我正在处理的应用程序包含2个源(.c)文件和1个头文件(.h)。我遇到的问题是在尝试将TurtleShell.c编译为" .o"时出现以下错误。文件:

[ahopkins@localhost TurtleShell]$ gcc -c TurtleShell.c -o TurtleShell.o
TurtleShell.c: In function ‘main’:
TurtleShell.c:12: error: ‘GetString’ undeclared (first use in this function)
TurtleShell.c:12: error: (Each undeclared identifier is reported only once
TurtleShell.c:12: error: for each function it appears in.)

通常,根据我的理解,这意味着我忘记声明一个放在main之后的函数,或者我忘了将我的头文件包含在#include" GetString.h"和/或头文件不与使用它的源文件驻留在同一目录(或/ usr / local / include,/ usr / local)中,但是,这三个文件确实驻留在同一目录和头文件中宣布。

需要注意的一点是,我试图通过函数指针访问GetString()。我过去在同一个源文件中使用过函数指针,但这是我第一次尝试使用多个源文件来处理使用函数指针的应用程序,所以我在这个过程中做了一些假设。源代码如下:

GetString.c:

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

/*
This function handles getting a string from the user by allocating each
character in the string to a char array. This array is guaranteed to
grow as large as it needs as well as trim itself down to only the needed
amount of memory to store the char array once the null terminator is
processed.
*/

int GetString(void)
{
    //Set initial array length reasonably. size_t is used due to it's ability
    //to allow an array to grow as large as it needs.
    size_t strLength = 32;
    char *stringPtr = malloc(strLength);
    if (stringPtr == NULL)
    {
        fprintf(stderr, "Unable to allocate memory to hold char array. Exiting!\n");
        return 1;
    }
    printf("Enter some input: ");
    int c = EOF;
    unsigned int i = 0;
    //Checks the value of c (user character input) to see if RETURN or CTRL+C/Z was entered
    while ((c = getchar()) != '\n' && c != EOF)
    {
        //Adds the character entered into the next index of the char array
        stringPtr[i++] = (char) c;
        //Check if we have reached the end of the allocated memory for the char array
        if (i == strLength)
        {
            //multiply the current amount of memory allocated by 2.
            strLength *= 2;
            if ((stringPtr = realloc(stringPtr, strLength)) == NULL)
            {
                fprintf(stderr, "Unable to expand memory to hold char array. Exiting!\n");
                return 2;
            }
        }
    }
    //End of input. This adds the null terminator to terminate the char array
    stringPtr[i] = '\0';
    //Check if we have any unused memory allocated for the array left. If so, we
    //shrink it down to be the size of the input including the null terminator
    if (sizeof(stringPtr) < strLength)
    {
        stringPtr = realloc(stringPtr, i);
    }
    printf("\n\nString value: %s\n\n\n", stringPtr);
    //Memory cleanup time
    free(stringPtr);
    stringPtr = NULL;
    return 0;
}

TurtleShell.c:

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

int main(void)
{
    int running = 1;
    while(running)
    {
        //Create a function pointer for GetString() so we can manipulate the outputted string from GetString
        int (*GetStringPtr)(void);
        GetStringPtr = &GetString;
        char *string = malloc(GetStringPtr());
        free(string);
        string = NULL;
    }
}

GetString.h:

#ifdef GETSTRING_H_INCLUDED
#define GETSTRING_H_INCLUDED
extern int GetString(void);
#endif

我知道这与我尝试创建函数指针的方式有关,就好像我将TurtleShell.c更改为以下格式,它按预期工作:

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

int main(void)
{
    int running = 1;
    while(running)
    {
        char *string = malloc(GetString());
        free(string);
        string = NULL;
    }
}

我在C中非常环保,因为我只写了大约3个星期,所以我可能会遗漏一些明显的东西。我用谷歌搜索过这个问题,但我发现要特别难以正确地说出这个问题才能找到相关的结果。非常感谢任何帮助。

PS - 我很清楚我可以使用很多好的GetString()类型函数,但是,这是为了学习,所以我正在根据需要构建自己的函数来增加教育这些练习的价值。

2 个答案:

答案 0 :(得分:1)

Getstring.h中的#ifdef应为#ifndef

答案 1 :(得分:1)

以下行没有意义:     char * string = malloc(GetString());

GetString()返回0,1或2。 这意味着您要分配长度为0,1或2个字节的内存。

如果你想获得你在GetString()中读过的字符串,请使用一个参数,一个指向char指针的指针。 在这种情况下,根本不需要函数指针。

int GetString(char** string)
{
  size_t strLength = 32;
  *string = malloc(strLength);
  ...
}

不要在函数末尾释放字符串。 在您的主要功能中执行此操作。 您可以通过将char *传递给他的函数来获取字符串:

char *string;
int ret = GetString(&string); // Get the address of 'string'