initialization

initialize a multidimensional C array of variable size to zero

一笑奈何 提交于 2021-02-06 10:18:06
问题 I want to initialize a two-dimensional array of variable size to zero. I know it can be done for a fixed-sized array: int myarray[10][10] = {0}; but it is not working if I do this: int i = 10; int j = 10; int myarray[i][j] = {0}; Is there a one-line way of doing this or do I have to loop over each member of the array? Thanks 回答1: You cannot initialize it with an initializer, but you can memset() the array to 0. #include <string.h> int main(void) { int a = 13, b = 42; int m[a][b]; memset(m, 0,

How do I initialize a two-dimensional List statically?

谁说我不能喝 提交于 2021-02-06 08:40:29
问题 How can I initialize a multidimensional List statically? This works: List<List<Integer>> list = new ArrayList<List<Integer>>(); But I'd like to init the list with some static lists like: (1,2,3), (4,5,6) and (7,8,9) 回答1: If you create a helper method, the code looks a bit nicer. For example public class Collections { public static <T> List<T> asList(T ... items) { List<T> list = new ArrayList<T>(); for (T item : items) { list.add(item); } return list; } } and then you can do (with a static

Refer to an object being initialized within the initialization?

我是研究僧i 提交于 2021-02-05 12:09:35
问题 I have an object DataParameterInfo (DPI) with a couple of delegate methods that are used to move data from a DataReader into a POCO or to get values out of the POCO. Example: new DataParameterInfo<IBulletinPCN> { FieldName = "ChangeProcedure", ParameterName = "@ChangeProcedure", EntityName = "ChangeProcedure", DataType = SqlDbType.NVarChar, FieldType = FieldType.Other, PopulateEntity = (dr, e) => e.ChangeProcedure = dr.IsDBNull(dr.GetOrdinal("ChangeProcedure")) ? null : dr.GetString(dr

C++ initialize variable based on condition [closed]

北慕城南 提交于 2021-02-05 11:25:27
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . Improve this question I am currently trying to figure out how to initialize variables based on conditions. So this is the current code that I want to modify: int dimsOut[4]; dimsOut[0] = data->nDataVar(); dimsOut[1] = dims[0]; dimsOut[2] = dims[1]; dimsOut[3] = dims[2]; const size_t dataSize = data

Variable not initialized and hides in field

佐手、 提交于 2021-02-05 08:32:35
问题 I'm fairly new to java, so I don't understand why i'm getting an initialization error. In the code my integer "Memes" is saying that it might not have been initialized and that a local variable hides in field. private void totalUpdate(int yes){ if (yes==1){ int CompBot = Integer.parseInt(lblbotComp.getText()); int CompTop = Integer.parseInt(lbltopComp.getText()); int CompMid = Integer.parseInt(lblmidComp.getText()); int Memes = Memes + CompBot + CompTop + CompMid; lbltotalComp.setText(

how to initialize all elements of an array at one go when definition and declaration are separate?

徘徊边缘 提交于 2021-02-05 07:49:30
问题 when we write something like this int arr[5] = 0; or int arr[5] = {0}; there is no problem but when we do something like this int arr[5]; arr[5] = {0}; an error occurs. Any explanation for this ? 回答1: It is simply a part of the language definition that arrays can be initialised, but not directly assigned. You can do what you want in C99 using memcpy() with a compound literal: int arr[5]; /* ... */ memcpy(&arr, &(int [5]){ 0 }, sizeof arr); With GCC's typeof extension, you can add a little

javascript variable initialization shows NaN

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 05:51:10
问题 function sumArray(numbers){ var sum; for(var i in numbers){ sum += numbers[i]; } return sum; } console.log(sumArray([1,2,3,4,5])); Hi all, The outcome is NaN . However, if I initialize sum with sum = 0 , the outcome is 15. Why JS does not recognize the value type in the array and do the initialization for me? Why does it return NaN in the first case? Thanks 回答1: When a variable is declared within current scope, it is initialized with undefined value. var sum; // is initialized with undefined

Will be initialized after warning fix

非 Y 不嫁゛ 提交于 2021-02-04 21:35:44
问题 good evening (and happy thanksgiving), I have the following code (ripped out of my main code into a standalone file) and am getting some warning messages that I'd like to resolve. Here is the code: #include <cassert> #include <ostream> #include <climits> #include <iostream> #include <string> using namespace std; class WordCount { public: // Constructors WordCount() : word(""), count(0) { } WordCount(string theWord, unsigned theCount = 1) : word(theWord), count(theCount) { } // Accessors

Will be initialized after warning fix

ⅰ亾dé卋堺 提交于 2021-02-04 21:30:21
问题 good evening (and happy thanksgiving), I have the following code (ripped out of my main code into a standalone file) and am getting some warning messages that I'd like to resolve. Here is the code: #include <cassert> #include <ostream> #include <climits> #include <iostream> #include <string> using namespace std; class WordCount { public: // Constructors WordCount() : word(""), count(0) { } WordCount(string theWord, unsigned theCount = 1) : word(theWord), count(theCount) { } // Accessors

Why does javac complain about not initialized variable?

核能气质少年 提交于 2021-02-04 16:59:06
问题 For this Java code: String var; clazz.doSomething(var); Why does the compiler report this error: Variable 'var' might not have been initialized I thought all variables or references were initialized to null . Why do you need to do: String var = null; ?? 回答1: Instance and class variables are initialized to null (or 0), but local variables are not. See §4.12.5 of the JLS for a very detailed explanation which says basically the same thing: Every variable in a program must have a value before its