+ 7

Hi.please help me understand structures in c programming. Someone can At least write a code for me with 3 columns...

23rd Jan 2018, 2:33 PM
Elizabeth muli
Elizabeth muli - avatar
9 Respostas
+ 6
thank you
25th Jan 2018, 7:00 AM
Elizabeth muli
Elizabeth muli - avatar
+ 5
thanks a lot I have understood😊
24th Jan 2018, 2:12 PM
Elizabeth muli
Elizabeth muli - avatar
+ 5
what is the difference between int main And void main??
24th Jan 2018, 2:16 PM
Elizabeth muli
Elizabeth muli - avatar
+ 3
the structure has the following format struct type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names; but in my example code, I'm not going to use the object_names...
24th Jan 2018, 8:45 AM
benthu
benthu - avatar
+ 3
#include <iostream> #include <cstring> using namespace std; struct Students { char first_name[20]; char last_name[20]; int student_id; }; int main() { //Create Students variables struct Students stdnt1; struct Students stdnt2; //Enter data into the variables strcpy(stdnt1.first_name, "Elizabeth"); strcpy(stdnt1.last_name, "Muli"); stdnt1.student_id = 3486277; strcpy(stdnt2.first_name, "Ben"); strcpy(stdnt2.last_name, "Thu"); stdnt2.student_id = 3550297; /* You can also fill in the data like: stdnt1. first_name = "Elizabeth"; it depends on your preference */ //Print the data cout << "Student 1 first name: " << stdnt1.first_name << "\nStudent 1 last name: " << stdnt1.last_name << "\nStudent 1 ID: " << stdnt1.student_id << endl; cout << "Student 2 first name: " << stdnt2.first_name << "\nStudent 2 last name: " << stdnt2.last_name << "\nStudent 2 ID: " << stdnt2.student_id << endl; return 0; }
24th Jan 2018, 8:46 AM
benthu
benthu - avatar
+ 2
You can also call a function to avoid repetition in the print data section: void printStruct(struct Students obj) { cout << "Student first name: " << obj.first_name << "\nStudent last name: " << obj.last_name << "\nStudent ID: " << obj.student_id << endl; } You can also use pointers: struct Students *obj = &stdnt1; then call the variables as usual: obj->first_name = "Elizabeth"; like for example, in the function above: void printStruct(struct Students *obj) { cout << "Student first name: " << obj->first_name << "\nStudent last name: " << obj->last_name << "\nStudent ID: " << obj->student_id << endl; } then to call it: printStruct (&stdnt1); you can also avoid writing struct in each declaration of a Students variable by defining the struct with the keyword: typedef e.g. typedef struct { char first_name[20]; char last_name[20]; int student_id; }Students; now to create Books variables: Students stdnt1, stdnt2; Now referring to the object_name that I told you about in the beginning, the "Students" I've written in the typedef example above is an object name, you can specify as many as you want....e.g. typedef struct { char first_name[20]; char last_name[20]; int person_id; } Students, Teachers; then to declare variables: Students stdnt1, stdnt2; Teachers teach1, teach2; this may not be very clear but if you practice on these and look at books about structures you'll surely learn more, hope this helps
24th Jan 2018, 8:47 AM
benthu
benthu - avatar
+ 1
so seems sololearn can't allow me to write a long text so I'm going to divide my answer
24th Jan 2018, 8:45 AM
benthu
benthu - avatar
+ 1
int and void is the return type of the main function...so when we write a c++ program we usually return 0 at the end of the main function and since 0 is an integer, the return type of main becomes int
24th Jan 2018, 3:03 PM
benthu
benthu - avatar
+ 1
oh, sorry i forgot...in c/c++, the runtime expects an int value to be returned when the program terminates i.e. 0 for successfully termination and a non-zero no. for an error, this may not be the case with other programming languages like java and python but it's an expectation from c/c++ programs...moral of the story, let main only return an int...trying to return any other value or void would not be a good idea for a good c/c++ program, considering the fact that the string is encoded and the IEEE 754 format adopted for floats are not simple hence if there is an easier route to accomplish a task, then their would be no good reason to go the other way
24th Jan 2018, 3:25 PM
benthu
benthu - avatar