我正在尝试在Ubuntu中运行一个C程序(使用gcc编译器(,由于某种原因,它不允许我使用strcpy函数。在下面的第二行代码中:
char test[10];
strcpy(test, "Hello!");
char c[2] = "A";
strcpy(test, c);
我收到以下错误:
testChTh.c:56:14: error: expected ‘)’ before string constant
strcpy(test, "Hello!");
^
testChTh.c:59:1: warning: data definition has no type or storage class
strcpy(test, c);
^
testChTh.c:59:1: warning: type defaults to ‘int’ in declaration of ‘strcpy’ [-Wimplicit-int]
testChTh.c:59:1: warning: parameter names (without types) in function declaration
testChTh.c:59:1: error: conflicting types for ‘strcpy’
In file included from testChTh.c:3:0:
/usr/include/string.h:125:14: note: previous declaration of ‘strcpy’ was here
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
我包含以下标头:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
我尝试在新文件中使用 strcpy,没有任何额外的内容,但出现相同的错误。我也尝试使用:
memset(test, ' ', sizeof(test));
在使用STRCPY之前,无济于事。
我已经检查了我所有的左括号,它们都有一个相应的结束(。此外,当我注释掉 strcpy 行时,错误就会消失。
任何见解都非常感谢。
char test[10];
strcpy(test, "Hello!");
char c[2] = "A";
strcpy(test, c);
如果我理解正确,您在文件范围内有这些行。行strcpy(test, "Hello!");
是一个语句,语句仅在函数体内部是合法的。由于编译器当时并不期望有语句,因此它尝试将该行解释为声明。
根据您的代码,以下内容是合法的(尽管它没有任何有用的作用(:
#include <string.h>
int main(void) {
char test[10];
strcpy(test, "Hello!");
char c[2] = "A";
strcpy(test, c);
}