Interview Questions
- How do you initialize a structure?
- What are the default values assigned to a Structure?
- How do you pass a Structure to a function?
- Will the values in a structure be modified when they are changed in a function?
- What is the -> operator used for in relation to Structure?
- What is the difference between a Structure and a Union?
- What is dynamic memory allocation?
- What if free is not called in the method above?
How do you initialize a structure?
Below example shows how to initialize a structure in C Language. If part of the structure is not initialized, they are assigned default values.
#include <stdio.h> struct Point{ int x; int y; }; int main() { struct Point p1 = {4,5}; }
What are the default values assigned to a Structure?
Below example program shows the default values which are assigned to structures. An automatic structure variable is not initialized (garbage values). An external/static structure variable is initialized with default values.
#include <stdio.h> struct Point{ int x; int y; }; struct Point externalPoint; void printPoint(struct Point p) { printf("x:%d y:%d",p.x,p.y); } int main() { struct Point automaticPoint; printPoint(automaticPoint);//x:1833861174 y:32767 <= GARBAGE printPoint(externalPoint); //x:0 y:0 <= DEFAULT VALUE 0 IS ASSIGNED }
How do you pass a Structure to a function?
Below example shows how to pass a structure to function. Function printPoint prints the content of Point p.
#include <stdio.h> struct Point{ int x; int y; }; void printPoint(struct Point p) { printf("x:%d y:%d",p.x,p.y); } int main() { struct Point p = {1,2}; printPoint(p);//x:1 y:2 }
Will the values in a structure be modified when they are changed in a function?
No. It will not be modified. Below is an example program.
#include <stdio.h> struct Point{ int x; int y; }; void modify(struct Point p) //Definition of Function { p.x = 50; } int main() { struct Point p1; p1.x = 20; modify(p1); printf("p1.x:%d",p1.x);//p1.x:20 }
The modified value in the function is NOT reflected in main. This is because structures are also passed by value. When modify function is called the structure p1 is copied into a new structure and passed as an argument to modify. The new structure is modified but the original structure p1 remains unchanged.
What is the -> operator used for in relation to Structure?
Below program shows an example of the -> operator. This is used with a structure pointer.
#include <stdio.h> struct Point{ int x; int y; }; void printPoint(struct Point *p) { printf("x:%d y:%d",p->x,p->y); } int main() { struct Point p = {1,2}; printPoint(&p);//x:1 y:2 }
What is the difference between a Structure and a Union?
A union allocates same memory for all the member variables. A structure provides individual memory for all its member variables.
What is dynamic memory allocation?
Dynamic memory allocation allows us to allocate memory while executing a program. In the below example, malloc call allocates memory to the program.
#include <stdio.h> #include <stdlib.h> struct Point{ int x; int y; }; void printPoint(struct Point *p) { printf("x:%d y:%d",p->x,p->y); } int main() { struct Point *pointPtr; pointPtr = (struct Point *) malloc(sizeof(struct Point) ); pointPtr->x = 10;pointPtr->y=20; printPoint(pointPtr);//x:10 y:20 free( pointPtr ); return 0; }
What if free is not called in the method above?
A memory leak results. Memory is not returned back to the Operating System. It is a good practice to always call the free method on all the pointer variables which are dynamically allocated memory in a C program.