declaration

C multiple single line declarations

社会主义新天地 提交于 2019-11-26 23:01:58
问题 What happens when I declare say multiple variables on a single line? e.g. int x, y, z; All are ints. The question is what are y and z in the following statement? int* x, y, z; Are they all int pointers? 回答1: Only x is a pointer to int; y and z are regular ints. This is one aspect of C declaration syntax that trips some people up. C uses the concept of a declarator , which introduces the name of the thing being declared along with additional type information not provided by the type specifier

Can a JavaScript object property refer to another property of the same object? [duplicate]

北慕城南 提交于 2019-11-26 22:09:31
This question already has an answer here: Self-references in object literals / initializers 23 answers I recently tried to create an object like this: var carousel = { $slider: $('#carousel1 .slider'), panes: carousel.$slider.children().length }; My intentions were to improve jQuery's selector performance by caching the results of $('#carousel1 .slider') in an object property, and to keep the code concise and relatively DRY. However, this didn't work. When the code executed, it threw an exception when trying to parse the value of panes , complaining that carousel was undefined. This makes

Difference between pointer to pointer and pointer to array?

回眸只為那壹抹淺笑 提交于 2019-11-26 20:26:37
问题 Given that the name of an array is actually a pointer to the first element of an array, the following code: #include <stdio.h> int main(void) { int a[3] = {0, 1, 2}; int *p; p = a; printf("%d\n", p[1]); return 0; } prints 1 , as expected. Now, given that I can create a pointer that points to a pointer, I wrote the following: #include <stdio.h> int main(void) { int *p0; int **p1; int (*p2)[3]; int a[3] = {0, 1, 2}; p0 = a; p1 = &a; p2 = &a; printf("p0[1] = %d\n(*p1)[1] = %d\n(*p2)[1] = %d\n",

How does the Java array argument declaration syntax “…” work?

一曲冷凌霜 提交于 2019-11-26 19:59:26
I have been writing java for a while, and today I encountered the following declaration: public static void main(String... args) { } Note the "dot dot dot" in the array declaration, rather than the usual bracket []. Clearly it works. In fact I wrote a small test and verified it works. So, I pulled the java grammar to see where this syntax of argument declaration is, but did not find anything. So to the experts out there, how does this work? Is it part of the grammar? Also, while I can declare function like this, I can't declare an array within a function's body like this. Anyway, do you know

How to initialize a vector in C++ [duplicate]

孤街醉人 提交于 2019-11-26 19:18:40
Possible Duplicate: C++: Easiest way to initialize an STL vector with hardcoded elements I want to initialize a vector like we do in case of an array. Example int vv[2] = {12, 43}; But when I do it like this, vector<int> v(2) = {34, 23}; OR vector<int> v(2); v = {0, 9}; it gives an error: expected primary-expression before ‘{’ token AND error: expected ‘,’ or ‘;’ before ‘=’ token respectively. With the new C++ standard (may need special flags to be enabled on your compiler) you can simply do: std::vector<int> v { 34,23 }; // or // std::vector<int> v = { 34,23 }; Or even: std::vector<int> v(2);

Why are variables declared with their interface name in Java? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-11-26 19:03:33
This question already has an answer here: What does it mean to “program to an interface”? 32 answers This is a real beginner question (I'm still learning the Java basics). I can (sort of) understand why methods would return a List<String> rather than an ArrayList<String>, or why they would accept a List parameter rather than an ArrayList. If it makes no difference to the method (i.e., if no special methods from ArrayList are required), this would make the method more flexible, and easier to use for callers. The same thing goes for other collection types, like Set or Map. What I don't

Private Methods in Objective-C, in Xcode 4.3 I no longer need to declare them in my implementation file ?

旧巷老猫 提交于 2019-11-26 18:50:59
I have a lot question marks tolling above my head. What I don't get is before xcode 4.3 I needed to declare forward declarations (for private methods) in my implementation file. Like in my .m file: // deleting this with xcode 4.3 the below code still does work // in previous versions i had to put this because otherwise the compiler can't find methodFirst @interface DetailViewController () - (void)methodFirst; - (void)methodSecond; @end @implementation DetailViewController - (void) methodSecond { // if i delete the forward declaration now adays i dont get a compiler error that he cant find

Spiral rule and 'declaration follows usage' for parsing C and C++ declarations

半城伤御伤魂 提交于 2019-11-26 18:50:29
This question follows this other question about C declarations . Reading the answer to this question, I read about the spiral rule and I also understood what "declaration follows usage" means. Ok so far. But then I read this declaration : char *(*(*a[N])())(); and I was wondering how to parse it with the "declaration follows usage" 'rule'. Especially for the array part. What I read is: (*(*a[N])()) is a function () returning a char * , then, dereferencing the following (*a[N])() // 1 is this 'function returning a char* ', and so 1 is a 'pointer to a function returning char * ' then I would say

Do inner functions in JavaScript get hoisted? How do the scoping rules apply to them?

限于喜欢 提交于 2019-11-26 18:30:12
问题 I thought that JavaScript doesn't have block scope, but function scope, and that declarations are hoisted from their block to the top of their parent functions. However, the following code does not work as expected: function one(a) { console.log("one called for " + a); if (a == 1) { function inner(b) { console.log("inner called for " + b); } inner(123); } inner(456); } one(1); one(2); one(3); The first one(1); call proceeds normally, without any errors, however the execution stops when the

C++ array size dependent on function parameter causes compile errors

跟風遠走 提交于 2019-11-26 18:25:56
问题 I have a simple function in which an array is declared with size depending on the parameter which is int. void f(int n){ char a[n]; }; int main() { return 0; } This piece of code compiles fine on GNU C++, but not on MSVC 2005. I get the following compilation errors: .\main.cpp(4) : error C2057: expected constant expression .\main.cpp(4) : error C2466: cannot allocate an array of constant size 0 .\main.cpp(4) : error C2133: 'a' : unknown size What can I do to correct this? (I'm interested in