SPL-2021 Section B
SPL-2021 Section B
(c ) “Two variables of the same structure can be copied/assigned one into another” – show
with an example.
Answer:
#include<stdio.h>
#include<string.h>
struct Person{
char name[50];
int age;
float height;
};
Int main()
{
struct Person person1 = {“John Doe”,22,5.5};
struct Person person2 ;
strcpy(person2.name,person1.name);
person2.age = person1.age;
person2.height = person1.height;
}
2. (a) What is the benefit of using a pointer instead of using a static array? Define pass by value
and pass by reference.
Answer:
There are several benefits of using a pointer instead of using a static array. They are:
i) Dynamic memory allocation: Pointers allow dynamic memory allocation.
ii) Dynamic resizing: Pointers allow us to resize the allocated memory block dynamically
using functions like ‘realloc()’.
iii) Reduce memory consumption: Using pointers can reduce memory consumptions
compared to static arrays.
iv) Data structure implementation: Complex data structures like linked lists, trees, graphs
etc. are implemented using pointers.
(b) Are the following declarations represents the same?
char some[10]; and char *some;
Answer:
In C, ‘char some[10]’ declares an array capable of holding 10 characters, allocating fixed memory
for immediate storage. Conversely, ‘char *some’ declares a character pointer, initially
uninitialized, serving as a reference that can be assigned to pointe to a character or a sequence
of characters dynamically allocated elsewhere in the code.
In C, the ‘reallco()’ function is used to reallocate memory for already allocated block of memory.
It takes two arguments: the pointer to the previously allocated memory block and the new size.
i) If the new size is smaller, it may truncate the block, and the excess data is lost.
ii) If the new size is larger, it may expand the block or move it to a new location if
necessary.
Example:
int* ptr = (int*) malloc(5*sizeof(int)); // allocating memory for the first time
(b) What is the difference between the preprocessor directives #include”filename” and
#include<filename>
Answer:
#include”filename”, searches for the file in the current directory and then in system directories,
while #include<filename> looks only in system directories. The former is used for user defined
headers, and the latter is for system or standard library headers.
(C) Write a program to add the elements of an array using dynamic memory allocation.
Answer:
#include<stdio.h>
#include<stdlib.h>
int main()
int n,sum = 0;
scanf("%d",&n);
for(int i = 0;i<n;i++)
{
printf("array[%d] : ",i);
scanf("%d",&array[i]);
sum += array[i];
free(array);
i) Structure of a C program.
Answer:
A C program comprises sections for comments and details, linking directives, global
declarations, the main function as the entry point, and additional functions. It begins
with comments, includes linking directives, declares constants and variables, and
organizes logic within the main function and other subroutines.
iii) Identifires
Answer:
Identifiers in C are names for program elements like variables and functions. They start
with letters or underscores, followed by letters, digits, or underscores. Case-sensitive
and non-keyword, well-chosen identifiers enhance code clarity and understanding.
Answer: