我想创建一个带有消息的数组.
$myArray = array('my message');
Run Code Online (Sandbox Code Playgroud)
但是myArray如果已经存在,使用此代码将被覆盖.
如果我使用array_push,它必须已经存在.
$myArray = array(); // <-- has to be declared first.
array_push($myArray, 'my message');
Run Code Online (Sandbox Code Playgroud)
否则,它会叮叮当当.
有没有办法让上面的第二个例子工作,没有先清除$myArray = array();?
正如Microsoft Dynamics AX 2009编程:入门一书中所述,需要在x ++中声明后添加分号:
只要第一行代码不是关键字,变量声明后的额外分号就是强制性的.分号告诉编译器变量声明已经结束.在分号后面不能声明新变量.
(直接从书中复制,不变,如果需要我会删除它)
但是,当我删除分号并运行作业时,绝对没有错误或问题:
static void Job1(Args _args)
{
str string1 = "STACKOVERFLOW";
;
print string1;
pause;
}
Run Code Online (Sandbox Code Playgroud)
就像
static void Job2(Args _args)
{
str string1 = "STACKOVERFLOW";
print string1;
pause;
}
Run Code Online (Sandbox Code Playgroud)
真的需要吗?我应该习惯使用它吗?
来自-link-的ANSI C语法给出了以下数组声明规则:
(1) | direct_declarator '[' type_qualifier_list assignment_expression ']'
(2) | direct_declarator '[' type_qualifier_list ']'
(3) | direct_declarator '[' assignment_expression ']'
(4) | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']'
(5) | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']'
(6) | direct_declarator '[' type_qualifier_list '*' ']'
(7) | direct_declarator '[' '*' ']'
(8) | direct_declarator '[' ']'
Run Code Online (Sandbox Code Playgroud)
现在我对这些问题有一些疑问:
以下两个函数原型之间的区别是什么:
void foo(int [*]); 和
void foo(int []);
谢谢.
我正在一起学习C语言和汇编语言.我注意到,与C相比,程序集是一种无类型语言,它需要在处理数据之前声明数据类型.但我也了解到,即使用C语言编写的代码也会首先编译成用汇编编写的代码,然后组装成目标代码.因此,这意味着我们在C或任何高级语言中使用的数据类型声明仅用于C编译器的简易性.它们对目标代码没有任何特殊影响.那是对的吗?
我收集的是类型声明告诉编译器可以对数据执行的操作,数据大小(在数据段中存储数据所需),可存储的最大和最小十进制数的大小.我这样说是对的吗?
类型声明还有其他好处吗?
compiler-construction types programming-languages declaration
这对我来说没有意义,但我有一种感觉,我看到了一个代码使用这个:
var abc = def || ghi;
Run Code Online (Sandbox Code Playgroud)
我的问题是,这有效吗?我们可以为变量声明添加条件吗?我想答案是否定的,但我心里想到的是,我曾在代码中看到类似的东西.
假设我们有这个块:
int (^aBlock)(BOOL) = ^(BOOL param) { ...
Run Code Online (Sandbox Code Playgroud)
我目前对此的理解是:第一个int是返回类型,(^aBlock)(BOOL)给出方法的名称和参数的类型,并且= ^(BOOL param)是块内的参数名称...再次加上参数的类型?
为什么语法必须列出参数类型两次?这两种类型可能会有所不同吗?
当我编译下面的代码时
#include<stdio.h>
int main()
{
int a;
int a = 10;
printf("a is %d \n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
test3.c: In function ‘main’:
test3.c:6:5: error: redeclaration of ‘a’ with no linkage
test3.c:5:5: note: previous declaration of ‘a’ was here
Run Code Online (Sandbox Code Playgroud)
但是,如果我将变量设为全局,那么它可以正常工作.
#include<stdio.h>
int a;
int a = 10;
int main()
{
printf("a is %d \n",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么两次声明相同的全局变量而不是错误,但对局部变量这样做是错误的?
我有两个公共课; 一个静态(DesktopOps),一个非静态(Args),我试图初始化main中静态类的静态变量.
我一直得到的错误信息是:
main.cpp:25: error: qualified-id in declaration before '=' token
Point DesktopOps::window_coords = Point(arg.topleft_x, arg.topleft_y);
^
main.cpp:26: error: qualified-id in declaration before '=' token
Point DesktopOps::window_dims = Point(arg.width, arg.height);
^
Run Code Online (Sandbox Code Playgroud)
这是一个MWE:
#include <opencv2/opencv.hpp>
using namespace cv;
struct Args{
int topleft_x, topleft_y, width, height;
Args(){
topleft_x = topleft_y = width = height = -1;
}
};
struct DesktopOps {
static Point window_coords;
static Point window_dims;
};
int main(){
Args arg();
Point DesktopOps::window_coords = Point(arg.topleft_x, arg.topleft_y);
Point …Run Code Online (Sandbox Code Playgroud) 在阅读标准时,我真的很惊讶地看到,实际上,名称在声明中是可选的:
struct Magic {};
int main() {
int; // well-formed
Magic; // well-formed
}
Run Code Online (Sandbox Code Playgroud)
对于那些有兴趣阅读标准的人来说,这是因为在简单声明中,init-declarator-list是可选的.这是一个演示.
所以我真的想知道为什么这是允许的.我为什么要这么做?这对我来说毫无意义.我的理由是,因为如果没有意义,为什么要允许呢?
int main() {
int y;
int x{ y = 5 };
//x is 5
}
Run Code Online (Sandbox Code Playgroud)
由于y = 5不是可计算的表达式,这怎么可能?
另外,为什么编译器或IDE不会抱怨main()不返回int?
declaration ×10
c++ ×3
arrays ×2
c ×2
definition ×2
axapta ×1
c++11 ×1
grammar ×1
javascript ×1
objective-c ×1
opencv ×1
parameters ×1
php ×1
push ×1
redefinition ×1
static ×1
types ×1
variables ×1
x++ ×1