如何将原始字符串文字R与固定宽度整数类型的格式宏一起使用?
例如
std::int64_t num = INT64_MAX;
std::printf(R"("name":"%s", "number":"%")" PRId64, "david", num); // wrong syntax
Run Code Online (Sandbox Code Playgroud)
输出应该是
"name":"david", "number":"9223372036854775807"
Run Code Online (Sandbox Code Playgroud)
R不允许使用转义序列代替
在 C 中,这完全可以正常工作:
char* Test = (char*) malloc(sizeof(char));
Test = "Hey";
Run Code Online (Sandbox Code Playgroud)
同时在 Cpp 中它向我抛出了这个错误:
char* Test = (char*)malloc(sizeof(char));
Test = "Hey";
Run Code Online (Sandbox Code Playgroud)
“const char *”类型的值不能分配给“char *”类型的实体
这两者有什么区别,为什么Cpp中的指针总是const并且以后不能修改?
考虑这段代码:
char *test() {
return "HELLO";
}
int main() {
char *p = test();
printf("%s\n", p);
}
Run Code Online (Sandbox Code Playgroud)
这会在没有警告的情况下进行编译,我猜是因为"HELLO"它没有存储在堆栈中。但这给了我一个警告:
char *test() {
char arr[] = "HELLO";
return arr;
}
int main() {
char *p = test();
printf("%s\n", p);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
字符串文字真的存储在称为字符串文字池的区域中吗?
如果是这样,存储在字符串文字池中的数据是否可以被视为全局数据?
从函数返回字符串文字总是安全的(因为它是全局的)?
#include "usefunc.h" //don't worry about this -> lib I wrote
int main()
{
int i;
string given[4000], longest = "a"; //declared new typdef. equivalent to 2D char array
given[0] = "a";
printf("Please enter words separated by RETs...\n");
for (i = 1; i < 4000 && !StringEqual(given[i-1], "end"); i++)
{
given[i] = GetLine();
/*
if (sizeof(given[i]) > sizeof(longest))
{
longest = given[i];
}
*/
printf("%lu\n", sizeof(given[i])); //this ALWAYS RETURNS EIGHT!!!
}
printf("%s", longest);
}
Run Code Online (Sandbox Code Playgroud)
它为什么总是返回8 ???
以下代码生成错误,我看不到问题.有人可以帮忙吗?
customer_array = [‘Ken’,’William’,’Catherine’,’Mark’,’Steve’,’Sam’]
customer_hash = {
‘Ken’ => ‘Fiction’,
‘William’ => ‘Mystery’,
‘Catherine’ => ‘Computer’,
‘Mark’ => ‘Fiction’,
‘Steve’ => ‘Sports’,
‘Sam’ => ‘Fiction’
}
# => customer_array.rb:6: syntax error, unexpected tSTRING_BEG , expecting '}'
# 'William' => 'Mystery'
# ^
Run Code Online (Sandbox Code Playgroud) 什么时候是a == b真的却a.equals(b)是假的,反之亦然?
我知道这equals()用于比较字符串和== 的值来检查两个变量是否指向String对象的同一个实例.但在这种情况下,这两者会有所不同吗?
我对这种行为感到很困惑:
int main(void) {
char *test_string = "test_string";
*test_string = 'a'; //segfaults
return 0;
}
Run Code Online (Sandbox Code Playgroud)
int main(void) {
char test_string[] = "test_string";
*test_string = 'a'; //OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我预计它应该有相同的工作,因为N1570, Section 6.3.2.1(p3)声明如下:
除非它是运算
sizeof符,_Alignof运算符或unary &运算符的操作数,或者是用于初始化数组的字符串文字,否则具有类型''数组'类型'的表达式将转换为类型为''指针的表达式键入''指向数组对象的初始元素,而不是左值.
为什么第一个段错?
在 go 模板中,我想用变量替换下面的字符串:
bot := DigitalAssistant{"bobisyouruncle", "teamAwesome", "awesomebotimagename", "0.1.0", 1, 8000, "health", "fakeperson@gmail.com"}
Run Code Online (Sandbox Code Playgroud)
说我想bobisyouruncle用变量替换input
在 js 中,这很简单:
bot := DigitalAssistant{`${input}`, "teamAwesome", "awesomebotimagename", "0.1.0", 1, 8000, "health", "fakeperson@gmail.com"}
Run Code Online (Sandbox Code Playgroud) 我得到了这些结果。我究竟做错了什么?
const char *c = "\0";
cout << (c == NULL); // false
cout << (c == nullptr); //false
Run Code Online (Sandbox Code Playgroud) max()我正在尝试使用Visual-studio 2022 版本 17.7.1 的功能
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cout << max("5", "4") << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
4
Run Code Online (Sandbox Code Playgroud)
当我将代码更改为:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cout << max("4","5") << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
5
Run Code Online (Sandbox Code Playgroud)
但是当我使用代码块时:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cout << max("5", "4") << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
5 …Run Code Online (Sandbox Code Playgroud)