Elements of a struct when I declare it with a value - c++

If I make a struct and in one of the elements of that struct I assign a value, whenever I declare that structure, will the element have the value instead of garbage?
For example:
typedef struct {
int var1 = 10;
int var2;
} coordinates_t;
Every time I create a variable of the type coordinates_t the element var1 of that variable will be 10?
If not, is there a way to do that or will I have to 0 every struct I create?
A code version of that question:
typedef struct {
int v1 = 10;
int v2;
} numbers_t;
numbers_t player1;
printf("%d", player1.v1);
//What will be the output?

By "valor" I assume you mean "value".
I also assume C++11, since that syntax is not allowed with C++03.
Yes, you're default initializing the int v1 with the value 10 every time you construct the struct.

_"Everytime I create a variable of the type coordinates_t the element var1 of that variable will be 10?"_
For c++ yes.
typedef struct {
int v1 = 10;
int v2;
} numbers_t;
int main() {
numbers_t player1;
printf("%d", player1.v1);
}
Output
10
Proof
For c the code fails to compile.

Related

Passing an array in struct initialization

I would like to create a struct which contains both an int and an array of int. So I define it like
struct my_struct {
int N ;
int arr[30] ;
int arr[30][30] ;
}
Then I would like to initialize it with an array which I have already defined and initialized, for example
int my_arr[30] ;
for (int i = 0; i < 30; ++i)
{
my_arr[i] = i ;
}
Then I thought I could initialize a struct as
my_struct A = {30,my_arr}
but it doesn't seem to work (gives conversion error).
P.s. and how would it work with a 2d array?
Arrays cannot be copy-initialised. This isn't particular to the array being member of a class; same can be reproduced like this:
int a[30] = {};
int b[30] = a; // ill-formed
You can initialise array elements like this:
my_struct A = {30, {my_arr[0], my_arr[1], my_arr[2], //...
But that's not always very convenient. Alternatively, you can assign the array values using a loop like you did initially. You don't have to write the loop either, there's a standard algorithm for this called std::copy.

How do I set a whole struct to null?

struct zaidejas {
int numeris;
int aiksteleje;
bool penketas;
};
int main(){
zaidejas z[12];
z = {};
}
I get an error in the line of z = {}:
error: assigning to an array from an initializer list
I have no idea how to fix the error. I would really appreciate any help.
You can't assign to an array, only initialize it when you define it, or copy to it once it have been defined.
I recommend initialization:
zaidejas z[12] = {};
In C++11, you can initialize the array as follows:
zaidejas z[12]{};
If you want to initialize a single element of your array, you can use:
z[0] = zaidejas{};
This is the aggregate equivalent of the (constructor-based) initialization that was available in earlier versions of C++
z[0] = zaidejas();
You should just write
zaidejas z[12] = {};
// ^^^^
This will value-initialize all the array elements, which in turn will zero-initialize each element's class data members.
How do I set a whole struct to null?
Like this:
zaidejas z = {};
For your array, you should do:
zaidejas z[12] = {};
You have 12 elements. If you want to set all of them to null, do something like this
int main(){
int number_elements = 12;
zaidejas z[number_elements ];
for (int i = 0; i < number_elements ; z++) z[i] = {}
}

Using a typedef to create a reference to an array

I am being asked to make the types of the loop control variables known via a typedef statement. The problem I am having is that I don't know how, or even if it is possible, to make a typedef to a reference to an array of 4 elements.
/*
Write a program to print the elements of ia. It should
use a range for to manage the iteration.
*/
int main()
{
int ia[3][4] = {
{4,3,2,1},
{1,2,3,4},
{3,1,4,2}
};
for (int (&p)[4] : ia) // This is the line I am talking about
for(int z : p)
cout << z;
return 0;
}
I am still very new to programming, and I cannot seem to find an answer to this question. Any advice/help regarding the usage of typedef you can offer would be appreciated.
If you are using at least C++11, which is implied by the range-for statement, you can turn to "using" instead of "typedef". It serves the same uses and more, and it has a less confusing syntax:
// Equivalent declarations
typedef int (&arrayRef)[4];
using arrayRef = int (&)[4];
// Usage
for (arrayRef p : ia) { ... }
Furthermore, with using you can template the declaration itself:
template<typename T, size_t n>
using arrayRef = T (&)[n];
for (arrayRef<int,4> p : ia) { ... }
You write a typedef the same way as you write a variable declaration, except that you replace the variable name with the name you want to give to the type, and you stick typedef in front, hence:
typedef int (&R)[4];
will declare R to be the type "reference to an array of 4 ints".

Array Initialization from a struct

I was wondering if there was a way to initialize an array out of a variable from a struct. Say you have a struct like this-
struct Test{
int Number;
};
And you wanted to initialize the int Number to become an array.
I've already tried this, and it doesn't work:
Test t1;
t1.Number = new int[3];
t1.Number[3] = 6;
I know ISO C++ forbids resizing arrays, but if there was a way to initialize the integer to be an array, that's not really resizing(isn't it?)
Also, vectors don't work inside of structs. I get a "Vector does not name a type" error.
P.S., I can't do this either:
struct Test{
int Number[5];
};
Because at that time I don't know the size of the array I want.
vector works just fine in structs:
#include <vector>
struct Test {
std::vector<int> Numbers;
};
I'm not sure what you're really trying to do but I think this comes close.
One trick to do this
struct Test {
int Numbers[1];
};
when you initialize the struct, you need to use your own allocation function.
struct Test *NewTest(int sizeOfNumbers) {
return (struct Test*)malloc(sizeof(struct Test) + sizeof(int)*(sizeOfNumbers - 1));
}
then, you will be able to access the numbers by using,
struct Test *test1 = NewTest(10);
test1->Numbers[0]...
test1->Numbers[1]...
test1->Numbers[9]...
The return value of new int[3] is an int* not an int. To make your code work you can do:
struct Test {
int* Number;
};
int main() {
Test t1;
t1.Number = new int[4]; // Note this should be 4 so that index 3 isn't out of bounds
t1.Number[3] = 6;
delete t1.Number;
return 0;
}
However you should really use a std::vector rather than a static array. Vectors work just fine inside structs:
#include <vector>
struct Test {
std::vector<int> Number;
};
int main() {
Test t1;
t1.Number.resize(4);
t1.Number[3] = 6;
return 0;
}
You can use a pointer to int -- i.e.,
struct Test{
int *Number;
};
Then you can assign this at any future time to point to an array of your preferred size:
t.Number = new int[5];
But as other posters have already said, std::vector, with a small "v", works fine; be sure to #include <vector> so the compiler knows what you're talking about.

C++ initialization of struct containing an array

I have a structure that more or less follows this pattern:
struct sTruct {
int count;
struct {
int A;
int B;
int C;
} array[]; //count is the size of this array
};
I would like to be able to initialize these with something like the following syntax:
sTruct gInit1 = { 2, { {1,2,3},{4,5,6} }};
Really, that initialization syntax (or rather, the compactness of it) is more important than the specific struct layout. I do not have access to the standard containers (embedded platform), but I might be able to replicate some of their behavior if needed.
In final form, I would like to initialize an array of roughly 300 of these sTruct containers at once, just to add one more level of parenthesis.
You can't do it. If you gave the array a size you could. An alternative might be:
template < int size >
struct sTruct
{
struct { int a, int b, int c } array[size];
};
sTruct<2> gInit1 = {{1,2,3},{4,5,6}};
But, of course, all your sTructs are different types and so it may not be what you want. Your only other alternative is going to have to be free-store based and won't give you that syntax until initialization lists in 0x.