【发布时间】:2024-01-11 15:38:01
【问题描述】:
我正在做一个小项目,以真正帮助我更好地理解 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 周,所以我可能遗漏了一些明显的东西。我已经用谷歌搜索了这个问题,但我发现很难正确地用词来找到相关结果。非常感谢任何帮助。
P.S - 我很清楚那里有很多好的 GetString() 类型函数我可能会使用,但是,这是用于学习的,所以我正在根据需要构建自己的函数以增加教育这些练习的价值。
【问题讨论】:
标签: c++ c function pointers header-files