Structures AND Pointers in C Tutorial
Table of Contents
Learn Structures AND Pointers in C MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
A C language Structure is a user define data type used to combine similar or different data types as one single entity.
An int variable contains only integer data. A float variable contains only real number data. But a structure can be defined to hold multiple type data and refer to the elements separately.
Structures and Pointers in C
Keyword used to define a Structure Data Type is ‘struct’.
Two operators namely DOT Operator and ARROW Operator are used along with structures to access elements.
Syntax of Structure
struct name { //different data type declarations //int year; }var_1, var_2, *var_3;
Structures AND Pointers Observations
- At the time of declaration itself, a structure may be given a name in order to reuse the structure data type.
- A structure may contain different types of data like int, float, double, char, arrays and pointers.
- If you do not want to reuse structure data type, you can define some STRUCTURE VARIABLES along with declaration it self without STRUCTURE NAME.
- Structure naming convention follows same rules of defining a variable name. It should not be a keyword.
- You can omit structure NAME declaring just structure variables like var_1 etc.
- and You should use DOT (.) operator to access structure elements or members like var_1.year
- You should use ARROW (->) operator to access structure elements if the variable is a POINTER, var_3->year.
- Important observation is that a STRUCTURE definition should END WITH A SEMICOLON (;).
structures and pointers in c
Example 1
#include<string.h> #include<stdio.h> int main() { struct mycar { char col[10], int year; }; struct mycar car1; strcpy(car1.col, "BLACK"); car1.year = 2019; printf("COLOR: %s, Year=%d", car1.col, car1.year); return 0; }
Structures AND Pointers in c
Example 2: Using a Structure and Pointer Variable
#include<string.h> #include<stdio.h> int main() { struct mycar { char col[10], int year; }*car5; //ARROW operator makes the difference. strcpy(car5->col, "RED"); car5.year = 2020; printf("COLOR: %s, Year=%d", car5->col, car5->year); return 0; }
C Structure elements in Memory
Declaring a structure does not allocate memory. Only when you create a structure variable, memory is allocated. Size of structure is the combined size of all data types in that structure.
struct mycar { char col[10], int year; }; //Size of structure = 10 bytes + 2 bytes = 12B
In the above example, size of a structure is the combined size of 10 char elements and one int element. It is 12 Bytes in total. Declaring this in a c program does not reserve memory of 12B.
struct mycar { char col[10], int year; }; struct mycar car1; //this line reserves memory of 12B
Creating a structure variable car1 reserves the actual memory required to hold all car1 elements.
Structures AND Pointers Note: To create a STRUCTURE VARIABLE, keyword struct is used again.
Structure elements are aligned in contiguous memory locations like storing arrays. To store the next element type of a structure in memory, memory location starting with a multiple of 4 or multiple of 8 is chosen. So there may be a gap of 3 bytes or 7 bytes if 1B element is stored first.
A preprocessor directive #pragma pack is used to tell the compiler to choose gap between two elements. #pragma pack(1) tells the compiler to store the element in next byte. So the gap of 3 bytes or 7 bytes may be avoided. This is called Packing of Structure Elements.
C Structure Initialization and Copying
Structure elements can be initialized either individually or in one go at a time.
struct mycar { char class; int year; float weight; }car1;
Initializing structure elements individually
car1.class = 'B'; car1.year = 2019; car1.weight = 350.5f; //kg
Initializing all structure elements in one go
struct mycar car2 = {'C', 2020, 275f }; // (OR) struct mycar car3; car3 = car2;
You can copy entire structure variable into another variable by using ASSIGNMENT OPERATOR ‘=’. You can initialize a structure variable using BRACES { } similar to initializing an array. Remember that you need to both declare and initialize elements at the same time using Braces
You can initialize all elements of a structure variable to Zeros or Null values using { 0 } definition. First element of the structure may be of any data type.
struct mycar car4 = { 0 }; // first element may be of any data type
Default value of a number data type is ZERO and character type data is NULL or ‘\0’.
Structure Variable Arrays in c
You can declare Structure variables like any other data type variables. You can use index starting from ZERO to refer to structure array variables.
struct student { char name[10]; int age; }stu[3]; stu[0].age = 25; stu[1].age = 27;
Nested Structures in c
You can nest a structure inside another structure. Accessing a nested structure member is achieved using the same DOT (.) operator and ARROW (->) operators. YOu can initialize the elements at the time of declaration of structure variable it self. Only caution is that you should maintain order of elements and their values between Braces or FLOWER BRACKETS.
int main() { struct engine { int capacity; }; struct mycar { struct engine eg; int model; }; struct mycar car1; car1.eg.capacity = 2; //Litre car1.model = 2019; //initializing in one go //maintain the order of elements struct mycar car2 = {3, 2020}; return 9; }
[WpProQuiz 39]
structures and pointers in c MCQ
1) What is a structure in C language.?
A) A structure is a collection of elements that can be of same data type.
B) A structure is a collection of elements that can be of different data type.
C) Elements of a structure are called members.
D) All the above
Answer [=] D
Explanation:
struct insurance { int age; char name[20]; }
2) What is the size of a C structure.?
A) C structure is always 128 bytes.
B) Size of C structure is the total bytes of all elements of structure.
C) Size of C structure is the size of largest element.
D) None of the above
Answer [=] B
Explanation:
Individually calculate the sizes of each member of a structure and make a total to get Full size of a structure.
3) What is the output of C program with structures.?
int main() { structure hotel { int items; char name[10]; }a; strcpy(a.name, "TAJ"); a.items=10; printf("%s", a.name); return 0; }
A) TAJ
B) Empty string
C) Compiler error
D) None of the above
Answer [=] C
Explanation:
Keyword used to declare a structure is STRUCT not structURE in lowercase i.e struct.
4) What is the output of C program.?
int main() { struct book { int pages; char name[10]; }a; a.pages=10; strcpy(a.name,"Cbasics"); printf("%s=%d", a.name,a.pages); return 0; }
A) empty string=10
B) C=basics
C) Cbasics=10
D) Compiler error
Answer [=] C
Explanation:
pages and name are structure members. a is a structure variable. To refer structure members use a DOT operator say a.name.
5) Choose a correct statement about C structures.
A) Structure elements can be initialized at the time of declaration.
B) Structure members can not be initialized at the time of declaration
C) Only integer members of structure can be initialized at the time of declaraion
D) None of the above
Answer [=] B
Explanation:
struct book { int SNO=10; //not allowed };
6) Choose a correct statement about C structure.?
int main() { struct ship { }; return 0; }
A) It is wrong to define an empty structure
B) Member variables can be added to a structure even after its first definition.
C) There is no use of defining an empty structure
D) None of the above
Answer [=] C
7) What is the output of C program.?
int main() { struct ship { int size; char color[10]; }boat1, boat2; boat1.size=10; boat2 = boat1; printf("boat2=%d",boat2.size); return 0; }
A) boat2=0
B) boat2=-1
C) boat2=10
D) Compiler error
Answer [=] C
Explanation:
Yes, it is allowed to assign one structure variables. boat2=boat1. Remember, boat1 and boat2 have different memory locations.
8) What is the output of C program with structures.?
int main() { struct ship { char color[10]; }boat1, boat2; strcpy(boat1.color,"RED"); printf("%s ",boat1.color); boat2 = boat1; strcpy(boat2.color,"YELLOW"); printf("%s",boat1.color); return 0; }
A) RED RED
B) RED YELLOW
C) YELLOW YELLOW
D) Compiler error
Answer [=] A
Explanation:
boat2=boat1 copies only values to boat2 memory locations. So changing boat2 color does not change boat1 color.
9) What is the output of C program with structures.?
int main() { struct tree { int h; } struct tree tree1; tree1.h=10; printf("Height=%d",tree1.h); return 0; }
A) Height=0
B) Height=10
C) Height=
D) Compiler error
Answer [=] B
Explanation:
Notice a missing semicolon at the end of structure definition.
struct tree { int h; };
10) Choose a correct statement about C structure elements.?
A) Structure components are kept in a variety of free memory locations.
B) structure elements are stored in register memory locations
C) structure elements are stored in contiguous memory locations
D) None of the above.
Answer [=] C
11) A C Structure or User defined data type is also called.?
A) Derived data type
B) Secondary data type
C) Aggregate data type
D) All the above
Answer [=] D
12) What are the uses of C Structures.?
A) structure is used to implement Linked Lists, Stack and Queue data structures
B) Structures are used in Operating System functionality like Display and Input taking.
C) Structure are used to exchange information with peripherals of PC
D) All the above
Answer [=] D
13) What is the output of C program with structures.?
int main() { struct tree { int h; int w; }; struct tree tree1={10}; printf("%d ",tree1.w); printf("%d",tree1.h); return 0; }
A) 0 0
B) 10 0
C) 0 10
D) 10 10
Answer [=] C
Explanation:
struct tree tree1={10};
Assigns the value to corresponding member. Remaining are set to Zero. So w is zero.
14) What is the output of C program with structures.?
int main() { struct tree { int h; int rate; }; struct tree tree1={0}; printf("%d ",tree1.rate); printf("%d",tree1.h); return 0; }
A) 0 0
B) -1 -1
C) NULL NULL
D) Compiler error
Answer [=] A
Explanation:
Easiest way of initializing all structure elements.
struct tree tree1={0};
15) What is the output of C program.?
int main() { struct laptop { int cost; char brand[10]; }; struct laptop L1={5000,"ACER"}; struct laptop L2={6000,"IBM"}; printf("Name=%s",L1.brand); return 0; }
A) ACER
B) IBM
C) Compiler error
D) None of the above
Answer [=] A
Explanation:
You can initialize structure members at the time of creation of Structure variables.
16) What is the output of C program with structures pointers.?
int main() { struct forest { int trees; int animals; }F1,*F2; F1.trees=1000; F1.animals=20; F2=&F1; printf("%d ",F2.animals); return 0; }
A) 0
B) 20
C) Compiler error
D) None of the above
Answer [=] C
Explanation:
Since F2 is a pointer to a structure variable, F2.animal is not permitted. So use ARROW operators. F2->animal.
17) What is the output of C program with structure arrays.?
int main() { struct bus { int seats; }F1, *F2; F1.seats=20; F2=&F1; F2->seats=15; printf("%d ",F1.seats); return 0; }
A) 15
B) 20
C) 0
D) Compiler error
Answer [=] A
Explanation:
With a structure variable, the DOT operator is utilised. Using a pointer to a structure variable and the ARROW operator.
18) What is the output of C program with structure arrays.?
int main() { struct pens { int color; }p1[2]; struct pens p2[3]; p1[0].color=5; p1[1].color=9; printf("%d ",p1[0].color); printf("%d",p1[1].color); return 0; }
A) 5 5
B) 5 9
C) 9 5
D) Compiler error
Answer [=] B
Explanation:
You can declare and use structure variable arrays.
19) What is the output of C program with structure array pointers.?
int main() { struct car { int km; }*p1[2]; struct car c1={1234}; p1[0]=&c1; printf("%d ",p1[0]->km); return 0; }
A) 0
B) 1
C) 1234
D) Compiler error
Answer [=] C
Explanation:
The creation of arrays of pointers to structure variables is permitted.
20) Choose a correct statement about C structures.
A) A structure can contain same structure type member.
B) The only constraint on a structure’s size is the computer’s physical memory.
C) You can define an unlimited number of members inside a structure.
D) All the above.
Answer [=] D