initialization Questions

5

Solved

I have structure struct ABC { int a; int b; } and array of it as struct ABC xyz[100]; I want to initialize it a = 10 and b = 20; for all array element. Which is better way ?
Bivens asked 3/2, 2014 at 13:10

2

Solved

I've never seen this call to char() as a function before. Where is this described and what does it mean? This usage is part of the example on this cppreference.com community wiki page: https:...
Phippen asked 24/5, 2022 at 17:31

3

I've just completed a CodeWars kata on vowel-counting in C & Rust. The sample code is easy & obvious. Arrays can be used as fast mappings. Here, I map characters to logical values (0 or 1)....
Littleton asked 22/5, 2022 at 17:42

9

Solved

Here is the code I'm using: #include <stdio.h> #include <stdlib.h> int main() { int *arr; int sz = 100000; arr = (int *)malloc(sz * sizeof(int)); int i; for (i = 0; i < sz; +...
Nerland asked 26/7, 2017 at 10:12

3

Solved

The following code is returning the compilation error below. I'm stuck understanding how there are too many initializers. This code works using vector<X>. Does anyone know why the error is be...
Salmonella asked 7/6, 2020 at 21:44

6

Solved

I've read a few Stack Overflow questions and answers, along with some blog posts (including Jon Skeet's lazy singleton initialization), and they all seem to focus on making initialization as lazy a...
Splenetic asked 22/3, 2013 at 18:52

7

Solved

In Java, is there any way to initialize a field before the super constructor runs? Even the ugliest hacks I can come up with are rejected by the compiler: class Base { Base(String someParameter)...

2

Solved

Brace initialization struct A { int a; int b; void foo(){} }; A a{1, 2}; It works fine. But if change foo to a virtual function, It will not compile with error, Error C2440 'initializing'...
Apron asked 24/6, 2021 at 8:26

7

Solved

I have a structure as follows: struct app_data { int port; int ib_port; unsigned size; int tx_depth; int sockfd; char *servername; struct ib_connection local_connection; struct ib_connecti...
Felty asked 4/7, 2015 at 1:27

3

Solved

After reading How to initialize an array in C, in particular: Don't overlook the obvious solution, though: int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; I tried something like this: ...
Synchro asked 22/5, 2012 at 1:27

4

Solved

Every now and then I need to call new[] for built-in types (usually char). The result is an array with uninitialized values and I have to use memset() or std::fill() to initialize the elements. Ho...
Dubiety asked 18/3, 2010 at 7:47

1

Solved

I am facing a relatively tricky situation here that seemed quite easy at first sight. After moving those three members from the parent class Parent to its child class Child it seems that I'm no lon...

16

Solved

How do you deal with a DateTime that should be able to contain an uninitialized value (equivalent to null)? I have a class which might have a DateTime property value set or not. I was thinking of i...
Haveman asked 21/10, 2008 at 12:54

6

Solved

I read about a static constructor in TypeScript and tried it myself but it doesn't work. I want to initialize a static variable by that (the method shall only be called once) but I get the followin...
Solenne asked 31/3, 2018 at 15:42

7

When compiling this code, I get the error "initializer element is not a compile-time constant". Can anyone explain why? #import "PreferencesController.h" @implementation PreferencesController - ...

5

Solved

I'm a beginner C programmer, yesterday I learned the use of C structs and the possible application of these ones about the resolution of specific problems. However when I was experimenting with my ...
Bantamweight asked 14/5, 2016 at 10:2

1

Solved

Sorry for the non-sense example. Tried to simplify it, still don’t get what’s going on. The following gives me Variable 'self.greeting' used before being initialized: struct MyView: View { @State ...
Bonne asked 13/2, 2022 at 13:31

14

Solved

I'm trying to find a convenient way to initialise 'pod' C++ structs. Now, consider the following struct: struct FooBar { int foo; float bar; }; // just to make all examples work in C and C+...
Starryeyed asked 30/5, 2011 at 23:59

1

Does C++ support a language construct that will allow us to initialize an object and all its padding fields to zero. I found some encouraging wording in cppreference.com about zero-initialization t...
Prepotent asked 3/2, 2022 at 22:17

22

Solved

Is there some way of initializing a Java HashMap like this?: Map<String,String> test = new HashMap<String, String>{"test":"test","test":"test"}; What would be the correct syntax? I...
Cramped asked 23/7, 2011 at 18:40

1

Solved

Background information about what inspired my question: I learned about Designated Initializers in C (see here and here: https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html), which are awesome...
Laoag asked 27/1, 2022 at 4:8

3

Solved

Here is a related C answer that doesn't work (as a zero initializer for a struct) in C++: Initializing a struct to 0. One of the solutions presented is this: myStruct _m1 = {0}; This works fine i...
Lamp asked 16/4, 2020 at 0:6

3

Solved

I recently came across a situation where my standard variable's values are replaced by the default one even if I have assigned a value with the constructor using init block. What I tried was: cl...
Harris asked 23/11, 2017 at 4:58

9

Solved

There are multiple answers/techniques to the below question: How to set default values to golang structs? How to initialize structs in golang I have a couple of answers but further discussion i...
Gazpacho asked 10/5, 2016 at 10:0

1

Solved

This code works, without having to specify a constructor: struct Foo { int a; int b; }; //... int a1, b1; Foo foo = {a1, b1}; If I make Foo a template, it doesn't work. template<typename...
Boric asked 19/1, 2022 at 7:16

© 2022 - 2024 — McMap. All rights reserved.