标签: variable-declaration

在循环内部或外部声明变量有很大区别吗?

例如

int i, a, c, d; // these vars will only be used in while
while(bigNumber--) {
    i = doSomething();
    // **

}
Run Code Online (Sandbox Code Playgroud)

while(bigNumber--) {
   int i, a, b, c; // i'd like to put it here for readability
   i = doSomething();
   // **
}
Run Code Online (Sandbox Code Playgroud)

它在性能方面有很大区别吗?

c performance variable-declaration

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

是否可以在 Rust 的 if 表达式的条件中声明变量?

在 Go 中,我们可以在表达式的条件中声明变量if。该变量在作用域内有效if,在作用域外无效。例如:

func main() {
    if n := 4; n != 0 {
        fmt.Printf("%d is not zero", n)
    } else {
        fmt.Printf("%d is zero", n)
    }

    fmt.Printf("%d", n) // error, n doesn't exist here!
}
Run Code Online (Sandbox Code Playgroud)

Rust 中有类似的语法吗?

scope if-statement variable-declaration rust

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

变量的声明和赋值在 K&R 中的不同行是否有原因?

我开始阅读The C Programming Language,我注意到变量的声明和它们的分配在不同的行上。例如,从第 16 页开始:

int c;
c = getchar();
Run Code Online (Sandbox Code Playgroud)

int c = getchar();没有不写的原因(一般来说,为什么赋值和声明不在同一行)?当我运行它而不是前者时,它似乎工作正常。

c variable-declaration

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

这里变量脱气的区别是什么?

我正在接受项目的维护和阅读代码:
我看到了两种变量声明方法.有人可以解释第一行和第二行之间的区别是什么意思吗?

对我来说,我在javascript中阅读,var关键字是可选的.在第一行中,他们声明了两个新变量并对其进行了初始化.在第二行中,他们声明了两个新的varialbes,但没有初始化它们.我应该从中获取更多吗?

aURL = ""; msgNb = 1;
var mode, param, counter;
Run Code Online (Sandbox Code Playgroud)

javascript variable-declaration

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

处理文件时出现奇怪的分段错误

我试图解析一个文件,我得到一个奇怪的分段错误.这是我正在使用的代码:

#include <iostream>

using namespace std;

int main ()
{
    FILE *the_file;
    the_file = fopen("the_file.txt","r");

    if (the_file == NULL)
    {
        cout << "Error opening file.\n";
        return 1;
    }

    int position = 0;
    while (!feof(the_file))
    {
        unsigned char *byte1;
        unsigned char *byte2;
        unsigned char *byte3;
        int current_position = position;

        fread(byte1, 1, 1, the_file);
    }
}
Run Code Online (Sandbox Code Playgroud)

我用命令编译它

g++ -Wall -o parse_file parse_file.cpp
Run Code Online (Sandbox Code Playgroud)

如果我删除while循环中声明current_position的行,代码运行没有问题.我也可以将该声明移到unsigned char指针的声明之上,代码将无问题地运行.为什么它会在声明中出现错误?

c++ file-io segmentation-fault variable-declaration

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

如何在if语句中声明值?(在java中)

所以这是我到目前为止,我不知道为什么程序没有响应我想要的方式.继续显示"avg2可能尚未初始化".有任何想法吗??

if (a < b && a < c) {
    System.out.println("The lowest score was: " + a);
    System.out.println("The average without the lowest score is: " + ((b + c) / 2));
    avg2 = ((b + c) / 2);
}

if (b < a && b < c) {
    System.out.println("The lowest score was: " + b);
    System.out.println("The average without the lowest score is: " + ((a + c) / 2));
    avg2 = ((a + c) / 2);
}

if (c < …
Run Code Online (Sandbox Code Playgroud)

java if-statement variable-declaration

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

声明变量的最佳位置在哪里?

我正在尝试用C++优化我已经实现的程序,并且想到了一个问题......如果我有一个使用它的重复结构,我应该在哪里声明一个变量?

例如:

//1-> int sum;
int matrix[10][10];

for(int i = 0; i < n; i++){
    //1-> sum=0;
    //2-> int sum=0;
    for(int j = 0; j < n; j++)
        sum += matrix[i][j];

    printf("%d ", sum);
}
Run Code Online (Sandbox Code Playgroud)

哪个更好/推荐?我应该在重复结构之外的开头// - > 1声明它,或者如果在每次迭代时重新声明// - > 2则没问题吗?我的猜测是声明需要很短的时间(毫秒或更短),但是如果你在一个足够大的重复结构(或在多个结构内)重新声明它,每次重新声明都会增加一些额外的时间.

c++ variable-declaration

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

类型推断:Java 7类型参数

今天我们谈到了Java 7在我们公司的优势.从Java 7开始,可以定义以下行

Map<String, List<String>> myMap = new HashMap<String, List<String>>();
Run Code Online (Sandbox Code Playgroud)

Map<String, List<String>> myMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

我们对上述主题进行了长时间的讨论.我们中的一些人认为这是类型推断(如varC#中的关键字),并且类型将在运行时计算,其他人认为它只是一种更简单的方式来声明某个变量并且没有推断,因为类型是已知的编译时的上下文.

请提供澄清.技术如何运作?

编辑:官方Oracle文档没有提供有关该文档的精确文档.http://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html

java generics type-inference variable-declaration java-7

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

如何声明由函数调用实例化的多个变量在Go中返回多个值?

假设我有一个功能:

func foo() (bool, string) { ... }
Run Code Online (Sandbox Code Playgroud)

然后我希望声明两个变量,bs使用函数调用返回的值进行初始化foo().我知道我可以使用省略类型注释的"速记"语法来做到这一点:

b, s := foo();
Run Code Online (Sandbox Code Playgroud)

但是,我不想使用这种速记语法.我希望使用var带有变量名和期望类型的语法.我试过这个:

var b bool, s string = foo();
Run Code Online (Sandbox Code Playgroud)

但是,这给了我一个语法错误.这样做的正确方法是什么?

syntax return-type go variable-declaration

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

编译时未定义的引用

我试图使用我已经在.c文件上的.h文件中声明的变量,我给了我一个编译错误:

未定义的引用 var

这是mach.c内容:

#include "machheader.h"

int 
main( void )
{
    var = 1;
    printf("Variable %d\n", var);
}
Run Code Online (Sandbox Code Playgroud)

而我的machheader.h只包含这个:

extern int var;

任何的想法?

c extern undefined-reference variable-declaration

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