我不明白这是如何隐式的,我在每个函数中返回一个 int ,并且所有函数都在主函数之前。老实说,第一次看到这个,有人给我发了很长的代码来调试,其中有很多错误,但为什么这不起作用,我错过了什么
int findString(char matrix[ROW][COLUNM],char str1[],int length){
int left,right,top,down,result;
left=left2_right(matrix,str1,length);-THESE 4 CALLS GIVE THE WARNINGS
right=rigth2_left(matrix,str1,length);
top=top_bottom(matrix,str1,length);
down=bottom_top(matrix,str1,length);
if(left != -1 ){result=left;}
if(right != -1 ){result=right;}
if(top != -1 ){result=top;}
if(down != -1 ){result=down;}
return result;
}
Run Code Online (Sandbox Code Playgroud)
这是其中一项功能
int left2_right(char matrix[ROW][COLUNM],char str1[],int length){
int i = 0, j, counting = 0, wordcnt;
//int length = computeLength(str1); //returns legth of string
int index = -1;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < …Run Code Online (Sandbox Code Playgroud) 我只是在玩 for 循环并写了下面的代码。当我编写第二个 printf 语句时,编译器要求我再次声明 c 变量,当我再次声明它时,我运行代码并
得到 c + 4 的答案为 20。for 循环工作正常。为什么 c + 4 表达式产生输出 20?
#include<stdio.h>
int main()
{
for(int c =1; c <= 10; ++c)
{
printf("%d\n",c);
}
int c;
printf("%d ",c+4);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我以为#1
uint8_t* start_ptr, end_ptr;
Run Code Online (Sandbox Code Playgroud)
和#2
uint8_t* start_ptr;
uint8_t* end_ptr;
Run Code Online (Sandbox Code Playgroud)
一般都是一样的。在我看来,他们不是。有人可以指定第一个除了第二个之外做什么吗?
发生了什么:
if(strncmp(mseq,mseq_a,8) == 0){
start_ptr = MY_UART_RingBuffer_getReadPointer();
start_found = 1;
}
if(strncmp(mseq+3,mseq_z,5) == 0){
end_ptr = MY_UART_RingBuffer_getReadPointer();
if (start_found == 1){
if(!MY_UART_RingBuffer_getOverlap()){
end_ptr = end_ptr - 6;
}
else{
start_found = 0;
continue;
}
ptrdiff_t length = end_ptr - start_ptr;
start_found = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
在使用 #1 时,编译器会给我这个长度计算:
../Core/Src/main.c:143:32: error: invalid operands to binary - (have 'int' and 'uint8_t * {aka unsigned char *}')
ptrdiff_t length = end_ptr - start_ptr; …Run Code Online (Sandbox Code Playgroud) 在Go中,我们经常用ifstatement和.声明来编写代码return err。像这样:
if res, err := getResult(); err != nil {\n return err\n } else {\n fmt.Println(res)\n // do something with res\n }\nRun Code Online (Sandbox Code Playgroud)\n但 linter 总是告诉我应该else在之后删除块return:
\xe2\x9a\xa0 https://revive.run/r#indent-error-flow if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)\nRun Code Online (Sandbox Code Playgroud)\n代码片段应如下所示以满足建议:
\n \xe2\x9a\xa0 https://revive.run/r#indent-error-flow if block ends with a return statement, so drop this else …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用<string>头文件,但我注意到还有<cstring>头文件。
两者有什么区别或者相同吗?
我知道我可以写
if (int a = 1; /* whatever */) {}
Run Code Online (Sandbox Code Playgroud)
乃至
if (int a = 1, b{3}; /* whatever */) {}
Run Code Online (Sandbox Code Playgroud)
但是我如何声明atypeint和bof type呢std::string?
这样的事情是行不通的:
if (auto a = 1, b{"ciaos"s}; /* whatever */) {}
Run Code Online (Sandbox Code Playgroud)
我没有包含标准,因为我对一般答案感兴趣,尽管实际上我会在c++17的上下文中使用答案。
而且,如果这样的事情不可能,是否有任何确切的原因(因此是language-lawyer)?
我想创建带有参数的方法,该参数链接到Enemy稍后声明的参数。\n这是我的代码:
#include <iostream>\n#include <vector>\nusing namespace std;\nclass Weapon{\n public:\n int atk_points;\n string name;\n string description;\n void Attack(Entity target){\n \n };\n};\nclass Armor{\n public:\n int hp_points;\n string name;\n string description;\n int block_chance;\n};\nclass Entity{\n public:\n int hp;\n int atk;\n string name;\n vector<Weapon> weapons;\n vector<Armor> armors;\n};\nRun Code Online (Sandbox Code Playgroud)\n我尝试寻找答案,但没有发现任何有用的信息。\n以下是错误日志:
\nprog.cpp:9:15: error: \xe2\x80\x98Entity\xe2\x80\x99 has not been declared\n void Attack(Entity target){\nRun Code Online (Sandbox Code Playgroud)\n 看下面的代码段,C语言中的数组已经是一个保存数组第一个元素地址的指针了,那为什么变量还要加星号呢argv?
char *argv[3];
argv[0] = "echo";
argv[1] = "hello";
argv[2] = 0;
exec("/bin/echo", argv);
printf("exec error\n");
Run Code Online (Sandbox Code Playgroud) 我对 C 很陌生,但我在一个看似非常微不足道的问题上遇到了麻烦。我想做的就是创建一个大小的数组,这样在运行时我不知道它的大小并且无法指定它。下面的代码运行得非常好。
int main()
{
int LuhnsArray[15] = {0};
for(int i = 0; i < 15; i++)
{
printf("%i ", LuhnsArray[i]);
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试这样的事情时:
int main()
{
int length = 15;
int LuhnsArray[length] = {0};
for(int i = 0; i < length; i++)
{
printf("%i ", LuhnsArray[i]);
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud)
我认为合乎逻辑的东西(例如,我认为在 python 中可以工作的东西)会附带一个错误。有人可以指出我正确的方向吗?新手有机会提供帮助吗?
假设test_t定义如下:
typedef struct test_t {
void *unused;
} *(test_t)
Run Code Online (Sandbox Code Playgroud)
是否可以将变量定义为指向 const 的指针而不修改 的定义test_t?
const test_t var将是一个指向 的 const 指针struct test_t,对吧?
我遇到这个问题,因为 sonarqube 建议“使该变量的类型成为指向 const 的指针”,但我无法更改定义,因为它在许多其他地方使用,其中变量应该是指向struct test_t.