declaration

Can I declare and initialize an array with the same instruction in Java?

梦想的初衷 提交于 2019-12-20 05:15:38
问题 Is there a way to do the following at the same time? static final int UN = 0; // uninitialized nodes int[] arr; // ... code ... arr = new int[size]; for (int i = 0; i < 5; i++) { arr[i] = UN; } Basically, I want to declare arr once I know what its size will be and initialize it to UN without having to loop. So something like this: int[] arr = new int[size] = UN; Is this possible? Thanks. 回答1: No, not with the standard libraries. If you write your own functions, though, you can easily do so in

How do I write a TypeScript declaration file for a complex external commonjs module that has constructor, such as imap?

谁说我不能喝 提交于 2019-12-20 04:30:17
问题 This question is a refinement of my earlier one: How do I write a TypeScript declaration file for an external commonjs module that has constructor? I'm trying to write a declaration file for the imap module: - imap on npm - node-imap What should this file look like? After not finding a satisfactory answer to my original question above, I realize there are some competing features of the language: any export or import makes a module an external module there appears to be no way to export

Declaring variables outside loop/IF structures in C

折月煮酒 提交于 2019-12-20 03:52:36
问题 I'm new to the C language, rather programming overall. I was wondering why is it that when I declare a variable to be used within an if statement OUTSIDE the structure, that the output I've received is incorrect (for this piece of code anyway). Here's my code: #include<stdio.h> void grossPay(); int main() { grossPay(); } void grossPay() { int rate = 10, hours; double tax, grosspay, netpay; printf("Enter work hours this week: "); scanf("%d", &hours); grosspay = hours * rate; if (grosspay <=

Why doesn't foward declaration of class work when class is included in another class

依然范特西╮ 提交于 2019-12-20 03:23:41
问题 This compiles #include "Sprite.h" class GameObject { public: int x, y, w, h; Sprite sprite; public: GameObject(); GameObject(int _x, int _y, int _w, int _h); virtual ~GameObject(); }; This doesn't class Sprite; class GameObject { public: int x, y, w, h; Sprite sprite; public: GameObject(); GameObject(int _x, int _y, int _w, int _h); virtual ~GameObject(); }; I know that I could forward declare and use pointer for Sprite but why doesn't forward declaration works here. Doesn't "class Sprite;"

Array declaration in FORTRAN for beginners

北战南征 提交于 2019-12-20 02:49:26
问题 This is a beginners question but I haven't found a comprehensive answer. What are the differences (if any) of the following declarations? CHARACTER(5) :: a CHARACTER, DIMENSION (5) :: b CHARACTER(LEN=5) :: c CHARACTER :: d(5) CHARACTER :: e*5 And: are all of these declarations possible with other types, e.g REAL ? 回答1: Regardless of the type, <type>,dimension(5) :: b and <type> :: b(5) are identical and denote an array of length 5 . <type> can be e.g. character , integer , real , logical ,

Variable already defined in .obj; What is going on here?

你离开我真会死。 提交于 2019-12-20 02:32:08
问题 head.h #pragma once namespace foo { int bar; int funct1(); } head.cpp #include "head.h" int foo::funct1() { return bar; } main.cpp #include <iostream> #include "head.h" int main() { foo::bar = 1; std::cout << foo::funct1() << std::endl; return 0; } Error LNK2005 "int foo::bar" (?bar@foo@@3HA) already defined in head.obj I don't understand what is going on. I tried looking for the answer but everyone's questions are so specific to their code and don't even look close to the problem that I am

Explanation of switch statement constraints on variably modified types in C standard

﹥>﹥吖頭↗ 提交于 2019-12-19 12:52:11
问题 I'm writing a C compiler, and when I come to the implementation of the switch statement one constraint confused me a lot. Section 6.8.4.2p2 of the standard states: If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier. With a footnote: That is, the declaration either precedes the switch statement, or it follows the last case or default label

What is the difference between the following declarations in C?

ぃ、小莉子 提交于 2019-12-19 10:33:58
问题 what's the difference between these 2 declarations: char (*ptr)[N]; vs. char ptr[][N]; thanks. 回答1: (1) declaration char (*ptr)[N]; ptr is pointer to char array of size N following code will help you to on how to use it: #include<stdio.h> #define N 10 int main(){ char array[N] = "yourname?"; char (*ptr)[N] = &array; int i=0; while((*ptr)[i]) printf("%c",(*ptr)[i++]); } output: yourname? See: Codepad (2.A) Where as char ptr[][N]; is an invalid expression gives error: array size missing in 'ptr

Making an undefined class as friend, and defining it later

久未见 提交于 2019-12-19 06:42:39
问题 Making an unknown friend template<typename T> class List { protected: class a { int x; int y; private: friend class b; // <------------ Why this is not an error? }; template <typename U > class b { //If that is not a error this should be an error int z; U y; }; public: List() { a* ptr = (a *)new unsigned char[sizeof(a)]; } }; int main() { List<int> mylist; } Please go through this link, I have my questions as comments in the code. I am trying to make another class a friend of my class. But

mixed declarations and codes

自作多情 提交于 2019-12-19 05:47:11
问题 When I compile function with "gcc -o dene -Wall -ansi -pedantic-errors dene.c" ,gcc emits no error.(can you look a line which starts with char ....,in if loop,) static void remove_negation(char *s,char *s1) { char **cmainp=malloc(sizeof(char*)*1); int len=0;int d=0; int i=0; cmainp[0]=malloc(sizeof(char)*300); len=strlen(s); for(i=0;i<len;++i) { if(s[i]=='-') if(i==0 || s[i-1]==',') /*look*/ {char *p=malloc(sizeof(char)*3); /*look*/ ++i; p[0]=s[i]; p[1]='\0'; strcat(s1,","); strcat(s1,p);