标签: declaration

-C 中的 W 隐式函数声明

我不明白这是如何隐式的,我在每个函数中返回一个 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)

c arrays declaration function

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

在c中声明同一个变量两次

我只是在玩 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)

c scope initialization declaration undefined-behavior

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

C中的指针声明,有什么区别?

我以为#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)

c pointers declaration

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

应该将 return err 放在带声明的 if 语句的 else 中,还是避免这种风格并减少 Golang 的返回值?

在Go中,我们经常用ifstatement和.声明来编写代码return err。像这样:

\n
    if res, err := getResult(); err != nil {\n        return err\n    } else {\n        fmt.Println(res)\n        // do something with res\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

但 linter 总是告诉我应该else在之后删除块return

\n
  \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)\n
Run 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)

if-statement lint declaration go golint

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

&lt;cstring&gt; 标头和 &lt;string&gt; 标头是否相同?

我正在尝试使用<string>头文件,但我注意到还有<cstring>头文件。

两者有什么区别或者相同吗?

c++ string header declaration c-strings

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

我可以在 if 语句中初始化不同类型的对象吗?

我知道我可以写

if (int a = 1; /* whatever */) {}
Run Code Online (Sandbox Code Playgroud)

乃至

if (int a = 1, b{3}; /* whatever */) {}
Run Code Online (Sandbox Code Playgroud)

但是我如何声明atypeintbof type呢std::string

这样的事情是行不通的:

if (auto a = 1, b{"ciaos"s}; /* whatever */) {}
Run Code Online (Sandbox Code Playgroud)

的上下文中使用答案。

而且,如果这样的事情不可能,是否有任何确切的原因(因此是)?

c++ if-statement initialization declaration language-lawyer

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

使用后的C++类声明

我想创建带有参数的方法,该参数链接到Enemy稍后声明的参数。\n这是我的代码:

\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};\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试寻找答案,但没有发现任何有用的信息。\n以下是错误日志:

\n
prog.cpp:9:15: error: \xe2\x80\x98Entity\xe2\x80\x99 has not been declared\n   void Attack(Entity target){\n
Run Code Online (Sandbox Code Playgroud)\n

c++ class declaration header-files

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

为什么数组变量需要星号?

看下面的代码段,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 arrays pointers declaration

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

如何在c中创建一个n大小的数组

我对 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 中可以工作的东西)会附带一个错误。有人可以指出我正确的方向吗?新手有机会提供帮助吗?

在此输入图像描述

c initialization declaration variable-length-array

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

当类型定义为结构体指针时,是否可以将 var 定义为指向 const 的指针?

假设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.

c pointers typedef constants declaration

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