So I'm having a very confusing issue where I'm attempting to print a string from a vector of arrays of structs to the console. Integers print just fine however strings stored within these structs get set to "". I have no idea what's going on here but after setting up a test as shown bellow this issue is still persisting. Any help figuring this out would be greatly apricated.
Should also mention I'm still new to c so I apologise if the issue here is something simple.
#include <iostream>
#include <string>
#include <vector>
#include "Header.h"
//Test struct
struct testStruct
{
string testString;
int testInt;
};
testStruct testArray[1] = {
testArray[0] = {"String works", 69}
};
int main()
{
srand(time(NULL));
vector < testStruct > test;
test.push_back({ testArray[0] });
cout << test[0].testString << "\n"; // prints "", should print "String works"
cout << test[0].testInt << "\n"; // prints 69
characterCreation();
checkPlayerStats();
introduction();
return 0;
}
CodePudding user response:
This surprised me. The following code is legal (syntactically at least)
testStruct testArray[1] = {
testArray[0] = {"String works", 69}
};
but if you replace it with the sensible version
testStruct testArray[1] = {
{"String works", 69}
};
then your program works as expected.
I expect your version has undefined behaviour because you are assigning (here testArray[0] = ...
) to an array element that has not yet been created.
CodePudding user response:
testStruct testArray[1] = {
This defines this array. This array, then, gets constructed. At what point, exactly, this array gets constructed is immaterial for the purposes of this question. It's sufficient to note that what goes inside { ... }
gets evaluated and used to construct this array.
testArray[0] = {"String works", 69}
This expression constructs the first value of the array. This expression assigns a value to testArray[0]
.
The problem is that testArray[0]
is not constructed yet, this is what's going on right now. This is undefined behavior. Just like what came first: the chicken or the egg. This is undefined.
You are seeing the results of undefined behavior, in the results of your program. The results of the program can be anything, and this just happens to be what shakes down, due to what your compiler and C library happen to produce, in terms of the executable code, before the dust settles.
CodePudding user response:
So first of all you need to use std::vector
, and std::cout
as you havent used using namespace std
.
But your main problem is:
testStruct testArray[1] = {
testArray[0] = {"String works", 69}
};
First of all it shouldnt be global as it does not need to be.
Second of all this is not correct:
testArray[0] = {"String works", 69}
You should not do this inside an array. What you probably meant to do is this:
testStruct testArray[1] = {{"String works", 69}}; // uses aggragate initialization.
So now this will have the correct output, with the following program:
#include <iostream>
#include <string>
#include <vector>
#include "Header.h"
//Test struct
struct testStruct
{
string testString;
int testInt;
};
int main()
{
testStruct testArray[1] = {{"String works", 69}};
srand(time(NULL));
vector < testStruct > test;
test.push_back({ testArray[0] });
cout << test[0].testString << "\n"; // prints "String works".
cout << test[0].testInt << "\n"; // prints 69.
characterCreation();
checkPlayerStats();
introduction();
return 0;
}
Assuming you have the Header.h
header file