当我试图在.h文件中声明一个带有字符串参数的函数时,会发生错误.我没有忘记包含string.h =)当我使用char []时,一切都很好,但我希望参数是一个字符串.
我想知道为什么当我尝试使用#define我从编译器获取错误来声明数组时,使用文字而不是大小允许我这样做.
#define size 10;
int* waitingBench[size];
Run Code Online (Sandbox Code Playgroud) 在主函数之前编写函数体而不声明正确运行.但是在main函数需要明确声明函数之后编写函数体.为什么?
由于指定的标准int a属于简单declaration.其实
simple-declaration:
decl-specifier-seq_opt init-declarator-list_opt ; //
attribute-specifier-seq decl-specifier-seq_opt init-declarator-list ;
type-specifier:
trailing-type-specifier //
class-specifier
enum-specifier
trailing-type-specifier:
simple-type-specifier //
elaborated-type-specifier
typename-specifier
cv-qualifier
simple-type-specifier:
nested-name-specifieropt type-name
nested-name-specifier template simple-template-id
char
char16_t
char32_t
wchar_t
bool
short
int //
long
signed
unsigned
float
double
void
auto
decltype-specifier
Run Code Online (Sandbox Code Playgroud)
因此int a是一个简单的声明.但是,如果我们重新声明a与以下相同的范围:
int a;
int a;
Run Code Online (Sandbox Code Playgroud)
我们有
test.cpp:4:5: error: redefinition of ‘int a’
test.cpp:3:5: error: ‘int a’ previously declared here
Run Code Online (Sandbox Code Playgroud)
到底究竟int a是什么?
int num1 = 8; //okay
int *pointer; //okay
*pointer = &num1; //NOT okay, compiler says (Error:a value of type int* cannot be
//assigned to an entity of type "int")
int num2 = 8; //okay
int *pointer = &num2; //okay
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么第一部分给出错误而第二部分没有,它们看起来和我一样
拿这个宣言:
int i = 80;
Run Code Online (Sandbox Code Playgroud)
这是我对用于描述每个部分的术语的理解(如果我错了,请纠正我):
int 是变量类型i 是声明者= 是赋值操作数什么术语用来形容80?
另一个例子:
char *c = "a";
// char - variable type
// c - declarator
// = - assignment operand
// "a" - ?
Run Code Online (Sandbox Code Playgroud) 我不太了解Map.Map部分.据我所知,当我导入限定为Map时,我需要将寻址前缀为"Map".但是通常后面跟着一个函数(例如"查找"),这里使用了两种类型.
import qualified Data.Map as Map
phonebook :: Map.Map String String
Run Code Online (Sandbox Code Playgroud)
我得到的电话簿需要一个字符串和一个字符串,但严格来说Map.Map表示它是一个关联列表,还是有更多呢?为什么省略箭头?
变量/函数可以声明多次,但只能定义一次。这实际上是什么意思?
我试图了解有关在线编译器的声明,但我期望显示错误,但事实并非如此。
#include <stdio.h>
int x=10;
int main() {
x=20;
x=30;
printf("%d",x);
}
Run Code Online (Sandbox Code Playgroud)
预期的输出:因为我已经定义了变量x并分配了三个不同的值10、20、30,所以我希望显示错误。这个概念说您可以声明变量多次,但只能定义一次,因为不能将两个不同的位置赋给同一变量实际输出:30
int fputc(int c, FILE *stream);
int fputs(const char *s, FILE *stream);
Run Code Online (Sandbox Code Playgroud)
为什么const int c在fputc()的声明中不需要?
declaration ×10
c ×5
c++ ×3
definition ×3
function ×3
pointers ×2
variables ×2
arrays ×1
haskell ×1
int ×1
java ×1
macros ×1
parameters ×1
types ×1