We can solve this problem easily by using structure.We can create a structure that has members for name, id, address and age and then we can create the variables of this structure for each student.However personally I always prefer a call to memcpy(), because it flags "here is a potentially long operation". The C Standard specifies nothing about the efficiency of the output of a piece of compiled C code.
Lets take an example to understand the need of a structure in C programming.
Lets say we need to store the data of students like student name, age, address, id etc.
-- Keith Thompson (The_Other_Keith) ks***@hello, please consider the following code: --- typedef lots int; //lots of data. The C Standard specifies nothing about the efficiency of the output of a piece of compiled C code.
typedef struct one_info; int main() --- from a performance point of view, is there anything that'd make me chose one of the options over the other? It's the purview of the compiler to optimise the object code. The program can, and should, be profiled for speed bottlenecks and then corrective action can be taken from a variety of angles.
This is useful when we are doing assignment of only few members of the structure.
In the following example the structure variable s2 has only one member assignment.typedef struct one_info; int main() --- from a performance point of view, is there anything that'd make me chose one of the options over the other? However most compilers are not naive and will inline and optimise small memcpy()s.Unless the compiler is pervesely written the furxt should never be less efficient than the last.This may sound confusing, do not worry we will understand this with the help of example. The struct keyword is a short form of structured data type. Lets say we have two structure like this: Structure 1: stu_address typedef makes the code short and improves readability.You can use a structure inside another structure, which is fairly possible. In the above discussion we have seen that while using structs every time we have to use the lengthy syntax, which makes the code confusing, lengthy, complex and less readable.As I explained above that once you declared a structure, the struct struct_name acts as a new data type so you can include it in another struct just like the data type of other data members. The simple solution to this issue is use of typedef. Code without typedef Instead of using the struct home_address every time you need to declare struct variable, you can simply use addr, the typedef that we have defined.We have already learned two ways to set the values of a struct member, there is another way to do the same using designated initializers.There's no real reason to assume that either is faster.It's possible that the assignment might be faster, because the compiler has more specific information available to it, but if the structure is large enough the difference is likely to be trivial. In most cases, it's best to write code to be as clear as possible, and worry about optimization only if you find that it's a real problem.In above structure programs, C structure is declared in main source file.Instead of declaring C structure in main source file, we can have this structure declaration in another file called “header file” and we can include that header file in main source file as shown below.