小编pel*_*o99的帖子

从普通指针中减去NULL指针会产生算术右移

这是C代码。

int main() {
  int i = 10, *p = &i;
  printf("%ld", p - (int *) NULL);
}
Run Code Online (Sandbox Code Playgroud)

对于指针算术部分,'gcc'和'clang'均在其汇编输出中生成'sar rax,2'指令。有人可以解释这种情况下的指针算术与算术右移有何关系。

c pointers null-pointer

6
推荐指数
1
解决办法
79
查看次数

C++ 连接;字符串+双;运算符+ vs 运算符+=

gcc v10.2.0,-std=c++11

我不想使用“std::to_string()”将双精度值转换为文字字符串。试图实现将整数添加到字符串但使用双精度值的类似效果。

预期输出:“abcdA”

string s { "abcd" };
double d { 65.1 };
// s = s + d;    // Error. no match for ‘operator+’ (operand types are ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} and ‘double’)
s += d;
Run Code Online (Sandbox Code Playgroud)

'string' 类的 'operator+' 和 'operator+=' 方法都有一个接受 'char' 参数的版本,但只有 'operator+=' 方法似乎接收隐式转换的值并且不会产生错误。

为什么编译器选择将转换后的值传递给另一个。

c++

1
推荐指数
1
解决办法
127
查看次数

标签 统计

c ×1

c++ ×1

null-pointer ×1

pointers ×1