Structure: Array VS Structure: Pointer and Structure:
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general format of a structure definition is as follows:
stuct tag_name { data_type member1; data_type member2; ———- —— ———– —– };
Array VS Structure:
Both the array and structures are classified as structured data types as they provides mechanism that enables us to access and manipulate data in relatively easy manner. But they are different in number of ways: 1. An array is a collection of related data element of same type. Structure can have elements of different types. 2. An array is derived data type whereas a structure is programmer-defined one. 3. Any array behaves like a built-in data type. All we have to do is to declare an array variable and use it. But in the case of a structure, first we have to design and declare a data structure before the variable of that type are declared and used.
Rules of initializing structure:
There are few rules to keep in mind while initializing structure variable at compile-time. 1. We cannot initialize individual members inside the structure template. 2. The order of values enclosed in braces must match the order of member in the structure definition. 3. It permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. The uninitialized members should be only at the end of the list. 4. The uninitialized members will be assigned default values as follows: • Zero for integer and floating point numbers. • ‘\0’ for character and string.
Note: Two variables of the same structure type can be copied the same ways as ordinary variables. And C programming doesn’t permit any logical operations on structure variables. In case, we need to compare them, we may do so by comparing members individually.
Structure and function: There are three methods by which the values of a structure can be transferred from one function to another. 1. The first method is to pass each member of the structure as an actual argument of the function call. The general format of sending a copy of a structure to the called function as: function_name(structure_variable_name);
The called function takes the following form: data_type function_name(Struct_type st_name) { — — — return (expression); }
The following points are important to note: 1. The called function must be declared for its type, appropriate to the data type it is expected to return. For example, if it is returning a copy of the entire structure, then it must be declared as struct with an appropriate tag name. 2. The structure variable used as the actual argument and the corresponding formal argument in the called function must be of the same struct type. 3. The return statement is necessary only when the function is returning some data back to the calling function. The expression may be any simple variable or structure variable or an expression using simple variables. 4. When a function returns a structure, it must be assigned to a structure of identical type in the calling function. 5. The called functions must be declared in the calling function appropriately.
Pointer and Structure:
Pointer used with simple variable (like type int and char ) provides increased power; the ability to do things in your program that are difficult or impossible any other way. The same is true of pointers used with structures.
Defining pointer to Structure:
Struct sturucture_name * pointer_variable;
Example: Struct complex s1,*s2; s2=&s1 //assign the address of the structure variable s1 to s2
Accessing member of structure using pointer variable: Pointer_variable -> member_name=value;
Example: s2->r=0;
Note: The . operator connects a structure with a member of the structure; the -> operator connects a pointer with a member of the structure.
