Tuesday, June 21, 2016

Structure Concepts

Structure Declaration:
A structure declaration that is not allowed by a list of variables reserves no storage;it merely describes a template or the shape of a structure.
Structure Initialization:
A structure can be initialisedas follows
struct point maxpt={320, 200};
An automatic structure may also be initialised by assignement or by calling a function that returns a structure of the right type
Structure Nesting:
Structures can be nested as follows
struct rect{
struct point pt1;
struct point pt2;}
rect strcuture contains two point structures.
Size of a structure:
Concept:We can use the concept of Pointer Difference
struct MyStruct
{
int i;
int j;
};
int main()
{
struct MyStruct *p=0;
int size = ((char*)(p+1))-((char*)p);
printf("\nSIZE : [%d]\nSIZE : [%d]\n", size);
return 0;
}
Structures and functions:
The only legaloperations on a structure are
1.copying it or assigning to it as a unit[Passing arguments to functions and returning values from functions]
2.Taking its address with &
3.Accessing its members
structures can be passed in functions in the follwing 3 ways
1.Pass Components separately
2.Pass an entire structure
3.Pass a pointer to it

No comments:

Post a Comment