C语言 无法编译:未知类型名称“字符串”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23523100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot compile: unknown type name 'string'
提问by Old Geezer
I have a simple C program:
我有一个简单的 C 程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argCount, string args[])
{
// ....
}
My make file is:
我的制作文件是:
TARGET = test
all: $(TARGET)
$(TARGET): $(TARGET).c
cc -g -std=gnu99 -o $(TARGET).out $(TARGET).c -lm
It gives a compilation error: unknown type name 'string' at the args parameter of main.
它给出了一个编译错误:main 的 args 参数中存在未知类型名称“字符串”。
What else must be included to be able to use string?
还必须包括什么才能使用字符串?
回答by luk32
There is no type named stringin c. C language use null-terminated array of characters as strings. So does the whole string.h.
stringc 中没有命名的类型。C 语言使用以空字符结尾的字符数组作为字符串。整个string.h 也是如此。
Look at any function definition e.g.: strlen- size_t strlen( const char *str );.
查看任何函数定义,例如:strlen- size_t strlen( const char *str );。
I guess you could use write a typedeffor it such as typedef char* string;but I would advise against it. It would introduce too much confusion in my opinion.
我想你可以使用 write a typedeffor it like,typedef char* string;但我建议不要这样做。在我看来,这会带来太多的混乱。
So your code should look like:
所以你的代码应该是这样的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argCount, const char* args[])
{
// ....
return 0; // don't forget it other wise your app will spit some random exit code
}
回答by AmazingCarpet
There is no type named string in c. If it's from school, there's probably an already included typedef somewhere of "string" in a header file:
c 中没有名为 string 的类型。如果它来自学校,则头文件中的“字符串”某处可能已经包含一个 typedef:
#define MAX_CHAR 100
typedef char string[MAX_CHAR+1];
#define MAX_CHAR 100
typedef char string[MAX_CHAR+1];

