Originally Posted by
Brian_Teir
if I want to access the member x , then I should write trajectory.x which it means accessing the variable x.
Unfortunately, that is not correct. In C, the struct declaration (you will also see it called a struct definition to differentiate it from a forward declaration of the struct) does not create an object of the struct unless you also immediately declare objects (or pointers, etc) of the struct type following its declaration. Furthermore, you cannot initialise the members of the struct in the way that you used. So, I would expect a struct declaration like this:
Code:
struct trajectory
{
int x;
int y;
};
Notice the terminating semi-colon for the declaration. Note that we would refer to this struct type as struct trajectory, not just trajectory. Given this struct declaration, we could declare an object of the type and initialise it as you intended:
Code:
struct trajectory bullet_trajectory = {6, 3};
Now, if you want to access the member x of the bullet_trajectory object, you would write bullet_trajectory.x, e.g.,
Code:
printf("%d\n", bullet_trajectory.x);
Originally Posted by
Brian_Teir
what's not comfortable with me is the syntax of writing the accessing of member x, i.e it should once I write trajectory.x then it means for me trajectory.6 because we called the variable x after the "." point of accessing the struct, so as we learnt the "return of reading variable" will return to its calling i.e we called the variable x after we write "trajectory. ", so the value of x should back to where we call the variable x which it should be like this trajectory.5 (trajectory.x) ... but apparently Im wrong because in my book mentioned something else that "trajectory.x" is implicitly like calling the variable x ..
Because the struct trajectory object is named bullet_trajectory and the member is named x, the int object that you're accessing is named bullet_trajectory.x. The initial value of this int object named bullet_trajectory.x is 6. bullet_trajectory.6 wouldn't make sense because 6 is not the name of a member of the struct; it happens to be the value of bullet_trajectory.x.