I am trying to cin a value to the birthMonth variable in my structure. My understanding is that structure members are stored sequentially. If that is the case, I cannot find out why using the line
cin >> *((short*)((people) + sizeof(Person::name)));
does not store the input value into the structure element. Am I wrong about the members of a structure being stored sequentially?
main.cpp
#include <iostream>
using namespace std;
struct Person {
char name[15];
short birthMonth;
};
int main() {
Person people[2];
cout << "Birth Month: ";
cin >> *((short*)(people + sizeof(Person::name)));
cout << people[0].birthMonth;
}
Sample Run:
Birth Month: 7
0
Program ended with exit code: 0