Chp 10 Structures Flashcards
Structure:
A structure contains a number of data types grouped together.
These data types may or may not be of the same type.
Define new Structure data type:
The foll. statement defines structure type: struct name { char name; int number; float f; char s[20]; };
this defines a new data type called struct name.
Each variable of this data type will consist of char variable name, int var number, float var f, array of char(string) s.
Declare variable of structure data type:
and initialisation?
struct name a,b,c ;
This statement sets aside space in memory.
we can also
struct name a = { ‘A’, 12, 20.0, “Alpha”};
Declaration of structure data type and the structure variables in 1 statement :
struct name { char name; int number; float f; char s[20]; } a, b, c ;
Rules
Precautions while declaring a structure type:
- Put a ; after closing braces.
- Structure type declaration just defines the form of a structure. It does not reserve any space.
- Usually, they happen at the beginning or in sep header file.
Accessing Structure elements:
structVarName.element
Here the use of dot operator is done for that purpose.
Before dot there must always be a structure variable and after dot there must always be a structure element.
How structure elements are stored:
Stored in contiguous memory locations.
Array of Structures:
Declare it:
struct name arr[100] ;
this provides space in memory for 100 structures of the type struct name.
(This really allows us to handle number of things which have types of data types.)
refer an element in array of structures:
and memory map of array of structures:
arr[92].name
arr[0]’s structure elements followed by arr[1]’s….
All adjacent.
linkfloat()
why is it used?
It forces linking of floating point emulator into an application. No need to call it, just define it.
In its absence “Floating point formats not linked” error occurs with majority of C compilers.
When parsing our source file, if the compiler encounters a float it sets a flag to have the linker link in the floating-point emulator, which manipulates floating-point numbers in runtime library functions like scanf(),atof().
Sometimes reference to float is obscure and the compiler does not detect the need for emulator.
Example: Using scanf() to read a float in an array of structures.
So here we force the formats to be linked.
linkfloat()
defined:
linkfloat()
{
float a=0,*b;
b = &a; / Cause emulator to be linked */
a = b; / suppresses earning, var not used */
}
Features of structures:
- Values of a structure variable can be assigned to another structure variable of the same type using the assignment operator. In array it wasn’t possible.
Which is possible as they have contiguous memory locations.
Nested structure, access the element?
suppose large is nested in struct bolt.
then
bolt.large.qty
will give us element qty within large struct which is in bolt.
Passing structure variable to function?
diaplay ( char *s, char *t, int n)
{}
Here call by value and call by reference both is done.
in case of char, base address is passed.
OR passing entire struct var at a time:
display ( struct name p)
{}
Make sure struct name is defined outside of any function so that display can access it.
Pointer to a structure:
when u have address, how to access structure element?
struct book *ptr;
ptr = &structbookVarname
printf(‘%s’, ptr->name);