我有一个播放器char player = x;,想要覆盖一个字符串char string[30];以包含"Player" + player + "won". 我试过
strcpy_s(string, "Player ");
strcat_s(string, player);
strcat_s(string, " won\n");
Run Code Online (Sandbox Code Playgroud)
但显然这不起作用,因为char不兼容const char
我该怎么做呢?
假设没有传递小于 4 个字节的字符串,那么这种优化有什么问题吗?是的,在比较大多数不相似的字符串时,在我测试过的机器上,这是一个显着的加速。
#define STRCMP(a, b) ( (*(int32_t*)a) == (*(int32_t*)b) && strcmp(a, b) == 0)
Run Code Online (Sandbox Code Playgroud)
假设字符串不少于 4 个字节,是否有一种更快的方法可以在不诉诸汇编等的情况下做到这一点?
我对这个简单的事情进行了尴尬的斗争,因为这是段错误:
#include <stdio.h>
int main()
{
char *test = "this is a string";
test[0] = 'q';
printf(test);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
#include <stdio.h>
int main()
{
char test[] = "this is a string";
test[0] = 'q';
printf(test);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
查看程序集后,我注意到在第一种情况下,文字"this is a string"是在 中声明的.rodata,因此这解释了段错误。但在第二种情况下,字符串根本不在程序集中,因此我假设它是通过 .data 部分链接为可写的。为什么会有这种行为差异?这很明显吗?我很愚蠢吗?
当我尝试用分隔符分割字符串时,该strtok函数不考虑空字符串,我想知道如何修复它。例如,就我而言,我必须拆分字符串,例如"a-b-c". 当我尝试拆分字符串时"a--b-c",结果应该是,,,, "a"(注意空字符串)。但如果我在调用函数后打印每个元素的结果,它只考虑、和。为什么会发生这种情况?我该如何解决?"""b""c""a""b""c"
char str[] = "a--b-c";
char *delim = "-";
char *token;
token = strtok(str, delim);
while (token != NULL) {
printf("element is: =====%s===== \n ", token);
token = strtok(NULL, delim);
}
Run Code Online (Sandbox Code Playgroud)
打印的元素应该是 4 ( a, 空字符串 , b, c) 但使用此代码,它只有 3 ( a, b, c)。
我试图了解数组地址的含义。
我写了下面的代码来尝试理解其含义,但我无法理解它:
char d [] {"Ashish"};
std::cout << d <<std::endl;
std::cout <<&d <<std::endl;
std::cout <<&d[0] <<std::endl;
std::cout <<(void*)&d[0] <<std::endl;
Run Code Online (Sandbox Code Playgroud)
每个语句的输出是什么?为什么第二条和第四条语句的输出相同?
在GDB中运行此程序,在通过target/replace malloc语句之后,[1]元素总是被赋予一个尴尬的值.
例如(使用GDB):
(gdb) p target[0]
$1 = -48 '\320'
(gdb) p target[1]
$2 = 101 'e'
(gdb) p target[2]
$3 = -4 '\374'
(gdb) p target[3]
$4 = -73 '\267'
Run Code Online (Sandbox Code Playgroud)
对于另一个变量,它的值是:replace [1] ='d'
它为什么这样做?如果我遗漏任何其他重要信息,请告诉我.
void replace(char** list, int wordLine, int targetAmount)
{
char** final;
char* target;
char* replace;
int wCounter, cCounter, i, hashCounter = 0, addLetter = 0;
int copyWord, countChars, numOfWords, finalWords = 0;
target = (char*)malloc(targetAmount); //allocating memory for
replace = (char*)malloc(targetAmount); //these char*'s
// other stuff …Run Code Online (Sandbox Code Playgroud) 我正在学习指针,我不明白指针如何与C风格的字符串一起使用.为什么这两个相当?
char a[] = "Gme";
char* p = a; //Why am I allowed to assign "Gme" to a pointer (pointer is an address)
cout << p << " " << *(p+1); //Why does it print "gme" with "cout<<p" (I mean, I am printing an address)?
Run Code Online (Sandbox Code Playgroud)
和
char a[] = "Gme";
char* p = &a[0]; // How is this the same as char* p = a;
cout << p << " " << *(p+1);
Run Code Online (Sandbox Code Playgroud)
总的来说,我不明白指针如何与字符串一起使用.如何将字符存储在内存中?如果我们将字符串视为字符数组,为什么我不能打印char元素的地址?
提前致谢 :)
为什么使用is_it_valid_color("yellow")工作和输出FOUND IT但使用is_it_valid_color(x.c_str());不起作用?
我有一种感觉它与null终止字符串有关.控制台中的输出看起来相同:
color: 'yellow'
FOUND IT
color: 'yellow'
Run Code Online (Sandbox Code Playgroud)
.
const char *color_names[] = {"yellow", "green", "red"};
const int color_names_SIZE = 3;
void is_it_valid_color(const char* color) {
cout << "color: '" << color << "'" << endl;
for(int i = 0; i < color_names_SIZE; ++i) {
if(color == *(color_names + i)) {
cout << "FOUND IT" << endl;
break;
}
}
}
is_it_valid_color("yellow");
string x = "yellow";
is_it_valid_color(x.c_str());
Run Code Online (Sandbox Code Playgroud) char buffer[2000];
char boundary[]= "--this-is-a-boundary\n";
char header1_a[]= "Content-Disposition: form-data; name=\"metadata\"\n";
char header1_b[]= "Content-Type: application/json; charset=UTF-8\n\n";
printf("%s%s%s\n\n\n\n", boundary, header1_a, header1_b);
std::memcpy(buffer, boundary, sizeof boundary);
std::memcpy(buffer + sizeof boundary, header1_a, sizeof header1_a);
std::memcpy(buffer + sizeof boundary + sizeof header1_a, header1_b, sizeof header1_b);
std::memcpy(buffer + sizeof boundary + sizeof header1_a + sizeof header1_b,
strJSONout, sizeof strJSONout);
printf("%s", buffer);
Run Code Online (Sandbox Code Playgroud)
但输出是:
--this-is-a-boundary
Run Code Online (Sandbox Code Playgroud)
字符串的其余部分会发生什么?我希望buffer包含所有这些字符数组......
是因为我复制了以NULL结尾的char数组吗?
我不明白为什么char *ptr = new char[7]不截断大于7个字符的数据输入。也是为什么fourr char c[7]允许我输入超过6个字符的信息(但将其字面值超过6个字符的属性赋予错误)。
对我来说,使用malloc函数执行此操作似乎有点困难,这就是为什么我不想使用它。我暂时不希望使用它。
char qs[7] = "1234567"; //error too many
char qs2[7];
cin >> qs2; //input 123456789
cout << qs2; //out same as input, expecting 123456
char *qs3 = new char[7];
cin >> qs3; //input 123456789
cout << qs3; //out same as input, expecting 123456
Run Code Online (Sandbox Code Playgroud)