-->

How to Access Structure Members

How to Access Structure Members
Sizeof Structure
We can use "sizeof operator to tell us the sizeof a structure (or any variable).
The expression
sizeof(struct x)
will evaluate the num number of bytes required to hold all the members of the structure x
If y is a simple structure variable of an array of type struct x, then
the expression is as shown:
sizeof(y)
would give the total number of bytes the array y requires.
The kind of information would be useful to determine the number of records in a database, for example the expression
sizeof(y) / sizeof(x)
would give the number of elements in the array 'y'.

Rules for Initializing Structures
1. We cannot initialize individual members inside the structure template.
2. The order of values enclosed in braces must match the order of members in the structure definition.
3. It is permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. The un-initialized members should be only at the end of the list.
4. The un-initialized members will be assigned default values as follows:
Zero for integers and floating point numbers
'\O' (null) for characters and strings

Accessing Structure Members
We can access and assign values to the members of a structure in a number of ways
The members themselves are not variables. They should be linked to the structure variables in order to make them meaningful
Example: salary has no meaning whereas "esalary of e3" has a meaning.
The link between a member and a variable is established using the "member operator", which is also known as "dot operator" or "period operator".
Example: "e3.esalary" is the variable representing the esalary of e3 and can be treated like any other ordinary variable.
Here is how we would assign values to the members of el.
strcpy(el. ename,"RAMU") ;
e1.eno=10;
e1.esalary=10000;
We can also use scanf to give the values through the keyboard.
scanf("%s %d %f", e1.ename,&e1.eno, &e1.esalary); is a valid input statement.

Related Posts

Subscribe Our Newsletter