Instead of initialising my variable in the struct with a numerical value of 0, I tried to use scanf such that it would read in my input values directly. It doesn't work but I can't figure out why?
I was wondering if it could be related to the fact that using &(p1.first) is invalid because the memory space had not been allocated? If so, is the only way to update the value of my struct variables via rewriting the scanf statements after the initialisation such as in the commented code below?
#include <iostream>
using namespace std;
typedef struct {
int first;
int second;
} score;
int main() {
score p1 = {
p1.first = scanf("%d", &(p1.first)),
p1.second = 0,
};
/* I know the following works:
score p1 = {
p1.first = 0,
};
scanf("%d", &(p1.first));
*/
printf("%d", p1.first);
}
When input is 2, I expected to get 2 as well, but regardless of the input value I tried, printf prints 1.