declaration

php: pushing to an array that may or may not exist

╄→гoц情女王★ 提交于 2019-12-05 02:49:55
I want to create an array with a message. $myArray = array('my message'); But using this code, myArray will get overwritten if it already existed. If I use array_push , it has to already exist. $myArray = array(); // <-- has to be declared first. array_push($myArray, 'my message'); Otherwise, it will bink. Is there a way to make the second example above work, without first clearing $myArray = array(); ? Check if the array exists first, and if it doesn't, create it...then add the element, knowing that the array will surely be defined before hand : if (!isset($myArray)) { $myArray = array(); }

Detecting declared package variables in perl

笑着哭i 提交于 2019-12-05 02:48:39
Given # package main; our $f; sub f{} sub g {} 1; How can I determine that $f , but not $g , has been declared? Off the cuff, I'd thought that *{main::g}{SCALAR} might be undefined, but it is a bona fide SCALAR ref. Background: I'd like to import a variable into main:: , but carp or croak if that variable is already declared. EDIT Added an f subroutine in response to @DVK's initial answer. ANSWER (2010-07-27) This isn't easy, but it is possible. An eval technique is most portable, working on perls older than 5.10. In more recent perls, introspective modules like Devel::Peek and B can

Get variable (not hard-coded) name?

大憨熊 提交于 2019-12-05 02:29:41
问题 I am looking for a way to retrieve the variable name, so I don't have to use hard-coded declarations when needed (for property names etc.): I hardly believe it's possible; maybe someone has a solution. Note: even not variables, properties would also be a move. 'Pseudo: Module Module1 Sub Main() Dim variable = "asdf" Dim contact As New Contact Dim v1 = GetVariableName(variable) 'returns variable Dim v2 = GetVariableName(contact.Name) 'returns Name End Sub Class Contact Public ReadOnly Property

Is Type name = name; ever useful in C++?

て烟熏妆下的殇ゞ 提交于 2019-12-04 23:05:59
The following code is allowed in C++: int a = a; or Type name = name; Both lead to an uninitialized object being initialized by itself, which often leads to undefined behavior. Is such code ever needed or reasonable? Are there cases of such code being useful? You are allowed to use the name of the variable in its initializer. The code Type name = name; is probably not useful, but the code Type name = f(&name); might be. There are many places where the language syntax doesn't forbid useless constructs. :-) adl This reminded me of an old thread of the GCC mailing list in which Gabriel Dos Reis

why change “complex*16” to “complex(16)” cause the runtime increased unreasonably in fortran?

我的未来我决定 提交于 2019-12-04 17:08:55
This fortran code was originally written in Fortran 77 format(I will show it later). After I got it, I changed it into f90 free format via a converting tool. Using intel fortran compiler ifort , the compiation and running is just as fine as before. Then I want to do more, I want to transform nonstandard,obsolete data type declaration f77 style like: real*8 , complex*16 etc into f90 standard real(8) , complex(16) . But I found an unbelievable thing. I just changed one "complex*16" into "complex(16)", then the running time increased from 10 seconds to 2 minutes. How could it be!!?? Can someone

C++ variable declaration and initialization rules

余生长醉 提交于 2019-12-04 15:08:42
Consider the following ways of declaring and initializing a variable of type C : C c1; C c2; c2 = C(); C c3(C()); C c4 = C(); Are all of these completely equivalent to each other, or can some of these differ depending on the exact definition of C ? (assuming it has public default and copy constructors). These mean: C c1; // default constructor C c2; // default constructor c2 = C(); // default constructor followed by assignment C c3(C()); // default constructor possibly followed by copy constructor C c4 = C(); // default constructor possibly followed by copy constructor Note the compiler can

Difficulty in understanding variable-length arrays in C

岁酱吖の 提交于 2019-12-04 13:42:27
I was reading a book when I found that array size must be given at time of declaration or allocated from heap using malloc at runtime.I wrote this program in C : #include<stdio.h> int main() { int n, i; scanf("%d", &n); int a[n]; for (i=0; i<n; i++) { scanf("%d", &a[i]); } for (i=0; i<n; i++) { printf("%d ", a[i]); } return 0; } This code works fine. My question is how this code can work correctly.Isn't it the violation of basic concept of C that array size must be declared before runtime or allocate it using malloc() at runtime.I'm not doing any of these two things,then why it it working

Is there a convention for pointer declarations in C? [closed]

 ̄綄美尐妖づ 提交于 2019-12-04 10:10:10
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . When declaring pointers in C, there are 3 variants: Variant A: int* ptr; Variant B: int *ptr; Variant C: int * ptr; In A, the

C double character pointer declaration and initialization

安稳与你 提交于 2019-12-04 07:35:34
问题 I always though that declaring char *c = "line"; was the same as char c[] = "line"; and so I did char **choices = { "New Game", "Continue Game", "Exit" }; Which gives me an incompatible pointer type, where char *choices[] = { "New Game", "Continue Game", "Exit" }; doesn't. Any help on understanding this? 回答1: Well, they're not the same. It's just easier for most people to think of them as being the same so everyone starts to think that way until they run into a problem like the above :-) I

Android: Issue during Arraylist declaration

青春壹個敷衍的年華 提交于 2019-12-04 05:47:54
问题 If I declare Arraylist like this- private ArrayList<Integer[]> nodeList; then, while adding array into it, getting NullPointerException But, if I change it to- private ArrayList<Integer[]> nodeList= new ArrayList<Integer[]>(); -it works fine. Why the first one fails! 回答1: The first only declares a variable, but does not create the actual object. only when you use new , you actually create the object. In java unlike C++, declaring a variable does not allocate a local variable of it. To