A union is called as a memory location, which is shared by several variables of different data types.SyntaxThe syntax is as follows −union uniontag{ datatype member 1; datatype member 2; ---- ---- datatype member n; };For example, union sample{ int a; float b; char c; };Declaration of union variableGiven below are the respective declarations of union variable −Union sample{ int a; float b; char c; }s;Union{ int a; float b; char c; }s;Union sample{ int a; float b; char c; }; union sample ... Read More
If the structure is nested inside a union, it is called as a union of structures. There is a possibility to create a union inside a structure in C programming language.ExampleFollowing is the C program for union of structures −#include struct x { int a; float b; }; union z{ struct x s; }; main ( ){ union z u; u.s.a = 10; u.s.b = 30.5; printf("a=%d", u.s.a); printf("b=%f", u.s.b); getch ( ); }OutputWhen the above program is executed, it produces the following result −a= 10 b = 30.5ExampleGiven below is ... Read More
Structure is a collection of different datatype variables, grouped together under a single name.Features of structureThe features of structure are explained below −It is possible to copy the contents of all structure elements of different datatypes to another structure variable of its type by using an assignment operator.For handling complex datatypes, it is better to create a structure within an another structure, which is called as the nested structures.It is possible to pass an entire structure, individual elements of a structure and an address of structure to a function.It is also possible to create the structure pointers.Declaration of structuresThe general ... Read More
The differences between structures and unions in C language are explained below −S.NoStructureUnion1DefinitionStructure is heterogenous collection of data items grouped together under a single nameDefinitionA union is a memory location that is shared by several variables of different datatypes.2Syntax;struct tagname{ datatype member1; datatype member2; ---- ---- ---- };Syntax;union tagname{ datatype member1; datatype member2; ---- ---- ---- };3Eg;struct sample{ int a; float b; char c; };Eg;union sample{ int a; float b; char c; };4keyword − structkeyword − union5Memory allocationMemory allocation67Memory allocated is the sum of ... Read More
ProblemIn C language, is the program executed, if we use an uninitialized array?SolutionIf we use any uninitialized array, compiler will not generate any compilation and an execution error.If an array is uninitialized, you may get unpredictable result.So, it’s better we should always initialize the array elements with default values.Example ProgramFollowing is the C program of accessing an uninitialized array − Live Demo#include int main(void){ int a[4]; int b[4] = {1}; int c[4] = {1,2,3,4}; int i; //for loop counter //printing all alements of all arrays printf("Array a:"); for( i=0; i
Suppose you have an array with four elements. Then, an array indexing will be from 0 to 3, i.e., we can access elements from index 0 to 3.But, if we use index which is greater than 3, it will be called as an index out of bounds.If, we use an array index which is out of bounds, then the compiler will compile and even run. But, there is no guarantee for the correct result.Result can be not sure and it will start causing many problems. Hence, it is advised to be careful while using an array indexing.Example ProgramFollowing is the ... Read More
Pointer to structure holds the address of an entire structure.Mainly, these are used to create the complex data structures such as linked lists, trees, graphs and so on.The members of the structure can be accessed by using a special operator called arrow operator ( -> ).DeclarationFollowing is the declaration for pointer to structure −struct tagname *ptr;For example, struct student *s;AccessingYou can access pointer to structure by using the following −Ptr-> membername;For example, s->sno, s->sname, s->marks;ExampleFollowing is the C program of the pointer structures −#include struct student{ int sno; char sname[30]; float marks; }; main ( ){ ... Read More
Passing the address of structure as an argument to function −The Address of the structure is passed as an argument to the function.It is collected in a pointer to structure in function header.AdvantagesNo wastage of memory as there is no need of creating a copy againNo need of returning the values back as the function can access indirectly the entire structure and work on it.Example#include struct date{ int day; int mon; int yr; }; main (){ struct date d= {02, 01, 2010}; display (&d); getch (); } display (struct date *dt){ printf("day = ... Read More
Passing individual members as arguments to function −Each member is passed as an argument in the function call.They are collected independently in ordinary variables in function header.Example#include //Declaring structure// struct student{ int s1,s2,s3; }s[5]; //Declaring and returning Function// void addition(int a,int b,int c){ //Declaring sum variable and For loop variable// int i,sum; //Arithmetic Operation// for(i=1;i
Passing entire structure as an argument to function −Name of the structure variable is given as argument in function call.It is collected in another structure variable in function header.DisadvantageA copy of the entire structure is created again wasting memoryProgramFollowing program demonstrates passing an entire structure as an argument to function − Live Demo#include //Declaring structure// struct add{ int var1; int var2; }a; //Declaring and returning Function// void show(struct add a){ //Declaring sum variable// int sum; //Arithmetic Operation// sum=a.var1+a.var2; //Printing O/p// printf("Added value is %d", sum); } void main(){ //Declaring structure// struct ... Read More