declaration

Variable definition inside switch statement

两盒软妹~` 提交于 2019-12-17 07:47:09
问题 In the following code, why is the variable i not assigned the value 1 ? #include <stdio.h> int main(void) { int val = 0; switch (val) { int i = 1; //i is defined here case 0: printf("value: %d\n", i); break; default: printf("value: %d\n", i); break; } return 0; } When I compile, I get a warning about i not being initialized despite int i = 1; that clearly initializes it $ gcc -Wall test.c warning: ‘i’ is used uninitialized in this function [-Wuninitialized] printf("value %d\n", i); ^ If val =

Semicolon after class declaration braces

主宰稳场 提交于 2019-12-17 07:13:15
问题 In C++ classes, why the semi-colon after the closing brace? I regularly forget it and get compiler errors, and hence lost time. Seems somewhat superfluous to me, which is unlikely to be the case. Do people really do things like: class MyClass { . . . } MyInstance; I get it from a C compatibility point of view for structs and enums, but since classes aren't part of the C language I guess it's primarily there the keep consistency between similar declaration constructs. What I was looking for

Easy rule to read complicated const declarations?

孤街浪徒 提交于 2019-12-17 07:01:53
问题 For reading complex pointer declarations there is the right-left rule. But this rule does not mention how to read const modifiers. For example in a simple pointer declaration, const can be applied in several ways: char *buffer; // non-const pointer to non-const memory const char *buffer; // non-const pointer to const memory char const *buffer; // equivalent to previous declartion char * const buffer = {0}; // const pointer to non-const memory char * buffer const = {0}; // error const char *

Easy rule to read complicated const declarations?

做~自己de王妃 提交于 2019-12-17 07:01:28
问题 For reading complex pointer declarations there is the right-left rule. But this rule does not mention how to read const modifiers. For example in a simple pointer declaration, const can be applied in several ways: char *buffer; // non-const pointer to non-const memory const char *buffer; // non-const pointer to const memory char const *buffer; // equivalent to previous declartion char * const buffer = {0}; // const pointer to non-const memory char * buffer const = {0}; // error const char *

What is the 'open' keyword in Swift?

我只是一个虾纸丫 提交于 2019-12-17 02:55:48
问题 The ObjectiveC.swift file from the standard library contains the following few lines of code around line 228: extension NSObject : Equatable, Hashable { /// ... open var hashValue: Int { return hash } } What does open var mean in this context, or what is the open keyword in general? 回答1: open is a new access level in Swift 3, introduced with the implementation of SE-0117 Allow distinguishing between public access and public overridability It is available with the Swift 3 snapshot from August

Where you can and cannot declare new variables in C?

天涯浪子 提交于 2019-12-17 02:27:50
问题 I heard (probably from a teacher) that one should declare all variables on top of the program/function, and that declaring new ones among the statements could cause problems. But then I was reading K&R and I came across this sentence: "Declarations of variables (including initializations) may follow the left brace that introduces any compound statement, not just the one that begins a function". He follows with an example: if (n > 0){ int i; for (i=0;i<n;i++) ... } I played a bit with the

How do I understand complicated function declarations?

早过忘川 提交于 2019-12-17 00:45:13
问题 How do I understand following complicated declarations? char (*(*f())[])(); char (*(*X[3])())[5]; void (*f)(int,void (*)()); char far *far *ptr; typedef void (*pfun)(int,float); int **(*f)(int**,int**(*)(int **,int **)); 回答1: As others have pointed out, cdecl is the right tool for the job. If you want to understand that kind of declaration without help from cdecl, try reading from the inside out and right to left Taking one random example from your list char (*(*X[3])())[5]; Start at X, which

Initializing multiple variables to the same value in Java

久未见 提交于 2019-12-16 23:40:16
问题 I'm looking for a clean and efficient method of declaring multiple variables of the same type and of the same value. Right now I have: String one = "", two = "", three = "" etc... But I'm looking for something like: String one,two,three = "" Is this something that is possible to do in java? Keeping efficiency in mind. 回答1: String one, two, three; one = two = three = ""; This should work with immutable objects. It doesn't make any sense for mutable objects for example: Person firstPerson,

Initializing multiple variables to the same value in Java

前提是你 提交于 2019-12-16 23:37:22
问题 I'm looking for a clean and efficient method of declaring multiple variables of the same type and of the same value. Right now I have: String one = "", two = "", three = "" etc... But I'm looking for something like: String one,two,three = "" Is this something that is possible to do in java? Keeping efficiency in mind. 回答1: String one, two, three; one = two = three = ""; This should work with immutable objects. It doesn't make any sense for mutable objects for example: Person firstPerson,

Initializing multiple variables to the same value in Java

和自甴很熟 提交于 2019-12-16 23:36:07
问题 I'm looking for a clean and efficient method of declaring multiple variables of the same type and of the same value. Right now I have: String one = "", two = "", three = "" etc... But I'm looking for something like: String one,two,three = "" Is this something that is possible to do in java? Keeping efficiency in mind. 回答1: String one, two, three; one = two = three = ""; This should work with immutable objects. It doesn't make any sense for mutable objects for example: Person firstPerson,