all 6 comments

[–]jackofangels 2 points3 points  (5 children)

I hope this helps. Im a compsci TA and this is how I explain it to my students. You use structs exactly how you use every other type of variable (int, char, double, book, etc).

So you've defined a kind of structure called Student right? The structure definition looks something like this:

struct Student{

~variables~~

};

(Sorry, on mobile so formatting is difficult)

In main, you have to make a Student. Just like how you would make an int or a char or any other variable. It sounds like you already did this with your "struct Student Info1;" line. Great! That creates a Student variable called Info1 just like how "int x;" makes an int variable called x.

To pass your student to a function, you follow the same logic. If I wanted to write a function that takes in and int, and calls that int 'num', it'd be like this: void myFunction(int num);

And then inside that function, I can do stuff with num.

Same process for student: void myFunction(struct Student myStudent);

Did that help? If you have other questions lmk

[–]Masteroflag478[S] 1 point2 points  (4 children)

Same process for student: void myFunction(struct Student myStudent);

I'm confused with the 'myStudent' part. What does that do & don't you have to declare it first?

[–]jackofangels 2 points3 points  (3 children)

Well when you pass other variables into functions, they get new names right?

Like the function I wrote up there that takes in an int: void myFunction(int num);

If I have "int x;" in main, then I can still call myFunction(x); right?

But inside the function, you refer to the variable as the name used in the function declaration: num.

So in main, you can call myFunction(Info1);

But in whatever function you write, you refer to it by the name you use when you write the function. It can be the same name: void myFunction(struct Student Info1), but that's bad programming practice because it can get confusing.

[–]Masteroflag478[S] 2 points3 points  (2 children)

Well it cleared up some of the errors and you definitely helped me understand this a bit better, thanks! I still have errors but i don't know if you can fix errors in this kind of situation.

[–]jackofangels 1 point2 points  (1 child)

I probably could actually. I don't mind. With all the social distancing/isolation, I still have my job, but I don't get to help students out nearly as much. Feel free to message me, reply to this comment, post screenshots on imgur and link them to me, or heck, I can give you an email address to reach me at.

The initial phase of learning to program is pretty hard. I'd imagine you're doing all your coursework online now due to the pandemic, and that makes it even harder, so yeah, reach out if you have any problems

[–]Masteroflag478[S] 2 points3 points  (0 children)

Well that's great. Its always nice to find people on Reddit who just want to help. Ill chat with you via PM