variable-declaration

Declaring vs Initializing a variable?

怎甘沉沦 提交于 2019-11-28 11:12:28
I'm curious to know the difference between declaring a variable, and initializing a variable. e.g. var example; // this is declaring var example = "hi" // initializing? Or just "adding a value"? I don't think I'm right there, but what exactly is the definition of each? Or do they basically mean the same thing? Edit: @ThisClark said something in the comments, and I went to prove him wrong, and upon reading the spec some more I learned something: Here's an informative except from the specification : A var statement declares variables that are scoped to the running execution context’s

Use of “var” type in variable declaration

我怕爱的太早我们不能终老 提交于 2019-11-28 09:36:02
Our internal audit suggests us to use explicit variable type declaration instead of using the keyword var . They argue that using of var "may lead to unexpected results in some cases". I am not aware of any difference between explicit type declaration and using of var once the code is compiled to MSIL. The auditor is a respected professional so I cannot simply refuse such a suggestion. How about this... double GetTheNumber() { // get the important number from somewhere } And then elsewhere... var theNumber = GetTheNumber(); DoSomethingImportant(theNumber / 5); And then, at some point in the

Is it possible to declare a public variable in vba and assign a default value?

五迷三道 提交于 2019-11-28 09:35:04
I want to do this but it won't compile: Public MyVariable as Integer = 123 What's the best way of achieving this? .NET has spoiled us :) Your declaration is not valid for VBA. Only constants can be given a value upon application load. You declare them like so: Public Const APOSTROPHE_KEYCODE = 222 Here's a sample declaration from one of my vba projects: If you're looking for something where you declare a public variable and then want to initialize its value, you need to create a Workbook_Open sub and do your initialization there. Example: Private Sub Workbook_Open() Dim iAnswer As Integer

Why does `int ;` compile fine in C, but not in C++?

拈花ヽ惹草 提交于 2019-11-28 08:51:39
Consider the following program (see live demo here ). #include <stdio.h> int main(void) { int ; // Missing variable name puts("Surprise"); } My compiler, gcc 4.8.1, gives the below warning: [Warning] useless type name in empty declaration [enabled by default] Why does it compile fine? Shouldn't I get a compiler error? g++ 4.8.1 gives the following error when I compile it as a C++ program: [Error] declaration does not declare anything [-fpermissive] The C standard says A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a

Find declaration of a global variable in PHP

本秂侑毒 提交于 2019-11-28 08:26:38
问题 Is there a quick and concise way of finding the declaration of a global variable in PHP? If we are dealing with a large codebase where the global variable may be referenced in many different files: Using ctags to jump to declaration takes you to the local statement where the global is brought into scope in the current file Searching for global $foo using grep or ack results in a list of files where the global was brought into scope, but not the exact declaration of the global variable Is

Should variable declarations always be placed outside of a loop?

余生长醉 提交于 2019-11-28 07:20:54
问题 Is it better to declare a variable used in a loop outside of the loop rather then inside? Sometimes I see examples where a variable is declared inside the loop. Does this effectively cause the program to allocate memory for a new variable each time the loop runs? Or is .NET smart enough to know that it's really the same variable. For example see the code below from this answer. public static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[32768]; while (true) { int

How can a variable be used when its definition is bypassed?

六月ゝ 毕业季﹏ 提交于 2019-11-28 07:11:34
In my mind, always, definition means storage allocation. In the following code, int i allocates a 4-byte (typically) storage on program stack and bind it to i , and i = 3 assigns 3 to that storage. But because of goto , definition is bypassed which means there is no storage allocated for i . I heard that local variables are allocated either at the entry of the function ( f() in this case) where they reside, or at the point of definition. But either way, how can i be used while it hasn't been defined yet (no storage at all)? Where does the value three assigned to when executing i = 3 ? void f()

Using a variable with the same name in different spaces

删除回忆录丶 提交于 2019-11-28 06:56:11
问题 This code compiles, but I have a run time error in Visual Studio: Run-time check failure #3 - the variable 'x' is being used without being initialized... int x = 15; int main() { int x = x; return 0; } I don't understand that behavior... in the error box when I click continue the program resumes and x has a corrupted content (like -8556328 instead of 15 ). Why does this code work without a problem, and the int array is well declared? const int x = 5; int main() { int x[x] = {1,2,3,4}; return

Is it well-formed, if I redefine a variable as auto, and the deduced type is the same? [duplicate]

前提是你 提交于 2019-11-28 03:18:01
问题 This question already has answers here : Does a declaration using “auto” match an extern declaration that uses a concrete type specifier? (3 answers) Closed last year . Look at this snippet: int a; extern int b; auto b = a; Is it well-formed? Clang successfully compiles it, but GCC and MSVC don't. (This issue has come up when I answered How to declare and define a static member with deduced type?) 回答1: Tl;DR; clang is correct, the logic is that this is allowed by [dcl.spec.auto] and to

Declaration of variable causes segmentation fault

本小妞迷上赌 提交于 2019-11-28 00:07:23
问题 I don't understand the reason for a segmentation fault error in my program. The code is available here At line 29 I declare a PclImage variable, defined with typedef like an array of struct. The definition of PclImage type is the following (from src/libMyKinect.h file): typedef struct { int valid; float x; float y; float z; unsigned char blue; unsigned char green; unsigned char red; } Point3d; typedef Point3d PclImage[480][640]; The program works well, but when I declare a second PclImage , I