指的js0n.c
代码语法如下:
static void *gostruct[] =
{
[0 ... 255] = &&l_bad,
['\t'] = &&l_loop, [' '] = &&l_loop, ['\r'] = &&l_loop, ['\n'] = &&l_loop,
['"'] = &&l_qup,
[':'] = &&l_loop, [','] = &&l_loop,
['['] = &&l_up, [']'] = &&l_down, // tracking [] and {} individually would allow fuller validation but is really messy
['{'] = &&l_up, ['}'] = &&l_down,
['-'] = &&l_bare, [48 ... 57] = &&l_bare, // 0-9
[65 ... 90] = &&l_bare, // A-Z
[97 ... …
Run Code Online (Sandbox Code Playgroud) 我遇到了这个奇怪的C++程序.
#include <iostream>
using namespace std;
int main()
{
int a = ({int x; cin >> x; x;});
cout << a;
}
Run Code Online (Sandbox Code Playgroud)
谁能解释一下发生了什么?这个结构叫做什么?
这是一段代码/usr/src/linux-3.10.10-1-ARCH/include/linux/printk.h
:
static inline int printk_get_level(const char *buffer)
{
if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
switch (buffer[1]) {
case '0' ... '7':
case 'd': /* KERN_DEFAULT */
return buffer[1];
}
}
}
Run Code Online (Sandbox Code Playgroud)
它是一种运营商吗?为什么"C编程语言"没有提到它?
我正在编写一些使用计算goto的代码.语法检查器正在标记每个实例goto *ptr
和&&label
语法错误.反正有没有阻止这个?
添加量:
计算得到的例子(gcc扩展名):
...
void * pLbl = NULL;
if (<some expression>)
pLbl = &&lbl1; /* gcc extension: no, '&&' is not a typo */
else if (<some other expression>)
pLbl = &&lbl2; /* gcc extension: no, '&&' is not a typo */
if (pLbl)
goto * pLbl; /* gcc extension: goes/jumps to either 'lbl1' or 'lbl2' */
goto lbl0;
lbl1:
<do some stuff>
goto lbl0;
lbl2:
<do some other stuff>
goto lbl0;
lbl0:
... …
Run Code Online (Sandbox Code Playgroud) 在我的C语言练习中,我面对一个表达式然后我简化如下:
int a=({10;});
Run Code Online (Sandbox Code Playgroud)
它是一个合法的表达式,因为它超越了gcc编译器.请关注这一部分:({10;})
.有人能解释一下吗?越详细越好.谢谢!
我一直认为c ++中不允许使用变长数组(参考:为什么变量长度数组不是C++标准的一部分?).但是为什么这段代码编译和工作?
#include <iostream>
using namespace std;
int main () {
int n;
cin >> n;
int a[n];
for (int i=0; i<n; i++) {
a[i] = i;
}
for (int i=0; i<n; i++) {
cout << a[i] << endl;
}
}
Run Code Online (Sandbox Code Playgroud) GCC有一个令人敬畏的三元表达式扩展到C,它允许我们创建一个这样的语句:
int x = some_var ?: 10; // expands to some_var ? some_var : 10
Run Code Online (Sandbox Code Playgroud)
这真的很好,虽然它不是特别直观,但确实有效.C语言中的大多数二元运算符都有一个与之关联的附加运算符,允许赋值:
x = x + 2;
// instead, we can say
x += 2;
Run Code Online (Sandbox Code Playgroud)
既然是这样的话,对于大多数二进制C运营商(常态+
,-
,*
,/
,%
,|
,&
,^
),为什么不是这样的三元扩展操作的情况下:
int x = ...;
x ?:= 2; // error: Expected expression before '=' token
// which would expand to
x = x ?: 2;
Run Code Online (Sandbox Code Playgroud)
唯一在标准C中不支持此操作符的运算符是逻辑运算符(||
,&&
),它们绝对不属于三元组,为什么我们不能这样做呢?
在我的代码中,我真的想用一个很酷的头发做一个笑脸,但我不能!这是设计操作员的疏忽,还是故意和记录在某处?是运营商将其操作数短路还是完全不同的结果?
我正在阅读C Primer Plus中的VLA,本书严格地说,将C语言引入到C的标准开始.每当我尝试在for循环的标头内声明循环控制变量时,gcc会通知我此操作仅在C99模式下允许.但是,以下测试代码编译并工作(虽然它打印垃圾变量,考虑到没有初始化的数组元素,这是预期的).
#include <stdio.h>
int main(){
int x;
int i = 9;
int array[i];
for(x = 0; x < i; x++)
printf("%d\n", array[x]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我不是C99模式,这怎么可能合法?
我正在尝试解决第三方库中的问题.问题是库使用GCC嵌套在宏中的嵌套函数,而Clang不支持嵌套函数,并且没有计划这样做(参见,Clang Bug 6378 - 错误:函数上的非法存储类).
这是我和Clang痛点的宏观:
#define RAII_VAR(vartype, varname, initval, dtor) \
/* Prototype needed due to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36774 */ \
auto void _dtor_ ## varname (vartype * v); \
void _dtor_ ## varname (vartype * v) { dtor(*v); } \
vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
Run Code Online (Sandbox Code Playgroud)
以下是它的使用方法(来自代码注释):
* void do_stuff(const char *name)
* {
* RAII_VAR(struct mything *, thing, find_mything(name), ao2_cleanup);
* if (!thing) {
* return;
* }
* if (error) {
* return;
* } …
Run Code Online (Sandbox Code Playgroud) #pragma pack(push,1)
有人能告诉我和之间的区别__attribute__((packed))
吗?如果我使用第二种类型的结构打包,我会收到编译错误
\n\n\nRun Code Online (Sandbox Code Playgroud)\ncannot bind packed field \xe2\x80\x98ABC.abc::a\xe2\x80\x99 to \xe2\x80\x98unsigned int&\xe2\x80\x99\n
但如果我使用第一种类型的结构打包,则不会出现编译错误。
\n\n这是我的示例代码:
\n\n//DataStructure.h\n\n#ifndef DATASTRUCTURE_H_\n#define DATASTRUCTURE_H_\n\nstruct abc\n{\n unsigned int a;\n float b;\n}__attribute__((packed));\n\n#endif /* DATASTRUCTURE_H_ */\n\n\n\n//Main.cpp\n#include<iostream>\n#include<map>\n#include "DataStructure.h"\n\n\nint main()\n{\n struct abc ABC;\n ABC.a=3;\n ABC.b=4.6f;\n\n std::map<unsigned int,struct abc> Mapp;\n Mapp.insert(std::make_pair(ABC.a,ABC));\n}\n
Run Code Online (Sandbox Code Playgroud)\n gcc-extensions ×10
c ×7
gcc ×6
c++ ×3
arrays ×1
clang ×1
eclipse ×1
eclipse-cdt ×1
macros ×1
syntax ×1