structure
Structure
Struct is keyword to declare structure
Structure definition
It is collection of dissimilar data element
Syntax
Struct structure_name
{ data type 1 var1;
Data type2 var2;
. .
Data type n var n;
}structure_variable ;
Declaring Structure Variables
It is possible to declare variables of a structure, either along with structure definition or after the structure is defined. Structure variable declaration is similar to the declaration of any normal variable of any other data type.
Example:
struct student
{
int mark;
char name[10];
float average;
}cse;
struct student
{
int mark;
char name[10];
float average;
}cse;
Declaration of tagged structure
struct tag_name
{ type1 var1;
type2 var2;
-------
typen varn;
}
example
struct student
{ int mark;
char name[10];
float average;
char name[10];
float average;
};
struct student cse;
Declaration of type defined structure
typedef struct
{ype1 var1;
type2 var2;
-------
typen varn;
}student;
example
typedef struct
{ int mark;
char name[10];
float average;
char name[10];
float average;
};
struct student cse;
Initializing structure :
cse = {100, “Adarsh”, 90.5};
Accessing structure members using normal variable:
cse.mark;
cse.name;
cse.average;
cse.mark;
cse.name;
cse.average;
Uses of structure
· Structure are used to represent more complex data structure
·
Self Referential Structures
Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member.
In other words, structures pointing to the same type of structures are self-referential in nature
In other words, structures pointing to the same type of structures are self-referential in nature
struct node {
int data1;
char data2;
struct node* link;
};
main()
{
struct node ob;
}
In the above example ‘link’ is a pointer to a structure of type ‘node’. Hence, the structure ‘node’ is a self-referential structure with ‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before accessing, as by default it contains garbage value.
An important point to consider is that the pointer should be initialized properly before accessing, as by default it contains garbage value.
In detail we will discuss in module 3
UNION
Unions are user-defined data type in C, which is used to store a collection of different kinds of data, just like a structure. However, with unions, you can only store information in one field at any one time.
· The keyword union is used to declare the structure in C.
· Variables inside the union are called members of the union.
Syntax
union [union tag] {
member definition;
member definition;
...
member definition;
} [one or more union variables];
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data d;
d.i = 10;
d.f = 220.5;
strcpy( d.str, "C Programming");
printf( "d.f : %f\n", data.f);
printf( "d.str : %s\n", data.str);
printf( "Memory size occupied by data : %d\n", sizeof(d));
return 0;
}
out put
d.i : 1917853763
d.f 412236058032759994368.000000
d.str : C Programming
Memory size occupied by data : 20