Unit - 4 Essay Questions
Unit - 4 Essay Questions
Program:
//To perform pointer concept application//
#include<stdio.h>
#include<conio.h>
int main()
{
int x=10, y=20, c, *p;
clrscr();
c=x+y;
p=&c;
Printf("sum=%d\n", *p);
getch();
return 0;
}
1.pointer increment (++): Incrementing a Pointer moves to the next memory location of the
same data type.
SYNTAX : pointer _variable_name++ ;
2. Pointer decrement(--): Decrementing a pointer moves it to the previous memory location
of the same data type.
SYNTAX : pointer _variable_name- -
3. pointer Addition(++): Adding an integer to a pointes moves it to a memory location that
is the specified number of elements away
SYNTAX : pointer_name + integer ;
4.Pointer subtraction(): Subtracting an integer from a Pointer moves it a memory location
that is the specified of elements away.
SYNTAX : pointer_name - integer ;
5. Pointer Difference : "subtracting one Pointes from another gives the number of elements
between them.
SYNTAX : pointer 1 - pointer 2 ;
3. How are generic pointers different from pointer variables and differentiate ptr++ and
++ptr.
Ans.
Generic pointers and pointer variables are both used to store memory addresses, but
they differ in their declaration and usage:
Example:
void* ptr;
int x = 10;
ptr = &x; // Store address of int variable
Pointer Variables :
•Declared using a specific data type (e.g., int*, char*, float*, etc.).
• Can only store addresses of variables of the same data type.
•The compiler performs type checking to ensure that the pointer is assigned a compatible
address.
•No explicit casting is required before dereferencing the pointer.
Example:
int* ptr;
int x = 10;
ptr = &x; // Store address of int variable
Post-Increment (ptr++)
•Returns the current value of the pointer.
•Increments the pointer to point to the next memory location after the operation is complete.
Example:
#include <stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int* ptr = arr;
printf("%d ", *ptr++); // Prints 1, then increments ptr
printf("%d ", *ptr); // Prints 2
return 0;
}
Pre-Increment (++ptr)
•Increments the pointer to point to the next memory location.
•Returns the new value of the pointer.
Example:
#include<stdio.h>
int main()
{
- `dataType` is the type of data that the pointers will point to (e.g., `int`, `char`, `float`)
- `arrayName` is the name of the array.
- `arraySize` is the number of elements in the array.
Explanation : An array of pointers is a collection of pointers that are stored in contiguous
memory locations. Each element in the array is a pointer that can point to a variable of the
same data type.
Example : int *ptrarray[3]; // Declare an array of 3 integer pointers
In this example, `ptrArray` is an array of 3 integer pointers. Each element in the array,
`ptrarray[0]`, `ptrarray[1]`, and `ptrArray[2]`, is a pointer that can point to an integer variable.
In this example, ptrarray[0] points to `var1`, ptrarray[1] points to `var2`, and `ptrArray[2]`
points to `var3`.
Accessing Elements Through an Array of Pointers : You can access the elements that
the pointers point to by using the dereference operator (`*`):
This code prints the values of `var1`, `var2`, and `var3` by dereferencing the pointers in the
`ptrArray`.
Dynamic Memory Allocation for an Array of Pointers : You can also dynamically allocate
memory for an array of pointers using `malloc()` or `calloc()`:
Eg:
int **ptrarray = (int **)malloc(3 * sizeof(int *));
5. Explain the term dynamic memory allocation and the terms malloc(), calloc(),
realloc() and free()
functions.
Ans.
Dynamic memory allocation: the process of allocating memory to variables during
execution of a program or at the runtime is known as "dynamic memory allocation." Thus c
program can either use static memory allocation or dynamic memory allocation
Eg: int arr[100];
Dynamic memory allocation uses standard library function <stdlib.h> to allocate memory
during the program run dynamic memory can be allocated in three ways.
(a)malloc():-This is used for allocating block of memory at runtime this function reserves a
block of memory of given size if it fails to allocate enough face as specified it returns a NULL
pointer.
Syntax: p=(cast_type*)malloc(n*bite_size);
Where 'p' is a point a type variable.
Eg: arr=(int*)malloc(10*sizeof(int));
(b)calloc():-it is normally used as a multiple blocks of memory each of same size and then
sets all bytes to zero calloc stands for continuous memory allocation it is primarily used to
allocate memory for arrays.
Syntax:
ptr=(cast_type*)calloc(n,byte_size);
Eg: arr=(int*)calloc(10,sizeof(int));
(c)realloc():-sometimes we may feel the memory allocated using malloc (or) calloc is in
sufficient (or) excess if we want to change the size of the allocated memory; can use
realloc() to add (or)
delete memory at the end of from the end of block of initial declared memory
Syntax:- p=realloc(p,newsize);
Where 'p' is the pointer type.
Eg: arr=realloc(arr,5);
6. Develop a C program using pointers to compute the sum of all elements stored in
array.
Ans.
PROGRAM :
//to calculate sum and to count even numbers in an array//
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],i,n,sum=0,*p;
printf("Enter the n value:");
scanf("%d",&n);
printf("Enter the %d array elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
p=a;
for(i=0;i<n;i++)
{
sum=sum+*(p+i);
}
printf("sum of elements is:%d\n",sum);
getch();
return 0;
}
In this example, `struct Student` is the name of the structure, and `rollNo`, `name`, and
`marks` are the variables that make up the structure.
Structure Initialization : Once a structure is declared, you can initialize its variables using
the dot (`.`) operator. There are two ways to initialize a structure:
1. Individual Initialization: You can initialize each variable individually using the dot
operator.
Example:
struct Student s1;
s1.rollNo = 1;
strcpy(s1.name, "John Doe");
s1.marks = 85.5;
```
2.Block Initialization: You can initialize all variables at once using a block.
Example:
struct Student s1 = {1, "John Doe", 85.5};
In this example, the variables `rollNo`, `name`, and `marks` are initialized with the values
`1`, `"John Doe"`, and `85.5`, respectively.
Accessing Structure Members : To access the variables of a structure, you use the dot
(`.`) operator followed by the name of the variable.
Example:
struct Student s1 = {1, "John Doe", 85.5};
printf("Roll No: %d\n", s1.rollNo);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
In this example, the variables `rollNo`, `name`, and `marks` are accessed using the dot
operator, and their values are printed to the console.
Program :
//To demonstrates structure declaration, initialization, and member access //
#include <stdio.h>
#include <string.h>
struct Student
{
int rollNo;
char name[50];
float marks;
};
int main()
{
struct Student s1 = {1, "John", 85.5};
struct Student s2 = {2, "Jane Doe", 90.0};
printf("Student 1:\n");
printf("Roll No: %d\n", s1.rollNo);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
printf("\nStudent 2:\n");
printf("Roll No: %d\n", s2.rollNo);
printf("Name: %s\n", s2.name);
printf("Marks: %.2f\n", s2.marks);
return 0;
}
In summary, structures are useful for grouping related variables of different data types,
while unions are useful for storing different types of data in the same memory space. Arrays
are useful for storing and manipulating collections of data of the same type.
9. Using structures, write a program to read and display the information of all the
students in a class.
Ans .
//to read and display the information of all students
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
char department[20];
int regdno;
int year ;
int semester;
};
int main()
{
int n, i;
printf("Enter the number of Students:");
scanf("%d",&n);
struct student s[n];
for(i=0; i<n; i++)
{
printf("Enter the information of %d student\n", i+1);
printf("Name: ");
scanf("%s", s[i].name);
printf ("department: ");
scanf("%s", s[i].department);
printf("Regd no: ");
scanf("%d", &s[i].regdno);
printf ("year: ");
scanf( "%d", &s[i].year);
printf(" Semester: ");
scanf("%d", &s[1].semester);
}
printf("\nStudent Information:\n");
for(i=0;i<n; i++)
{
printf("Student number %d details: \n",i+1);
printf("Name: %s\n", s[i].name);
printf("department: %s\n",s[i].department );
printf("Regd no: %d\n", s[i]. regdno);
printf("year: %d\n", s[i].year);
printf( "semester: %d\n", s[1].semester);
}
getch();
return 0;
}
10. Write a program using structures to read and display the information about the
entire faculty of a particular department.
Ans.
//to store department information using structures//
#include <stdio.h>
#include <conio.h>
struct Faculty
{
char name[50];
char designation[50];
char department[50];
char phone[20];
char email[50];
};
int main()
{
int n;
printf("Enter the number of faculty members: ");
scanf("%d", &n);
struct Faculty faculty[n];
for (int i = 0; i < n; i++)
{
printf("Enter information about faculty member %d:\n", i + 1);
printf("Name: ");
scanf("%s", faculty[i].name);
printf("Designation: ");
scanf("%s", faculty[i].designation);
printf("Department: ");
scanf("%s", faculty[i].department);
printf("Phone: ");
scanf("%s", faculty[i].phone);
printf("Email: ");
scanf("%s", faculty[i].email);
}
printf("\nFaculty Information:\n");
for (int i = 0; i < n; i++)
{
printf("Faculty Member %d:\n", i + 1);
printf("Name: %s\n", faculty[i].name);
printf("Designation: %s\n", faculty[i].designation);
printf("Department: %s\n", faculty[i].department);
printf("Phone: %s\n", faculty[i].phone);
printf("Email: %s\n\n", faculty[i].email);
}
getch();
return 0;
}
11. Write a program to find the smallest of three numbers using structures.
Ans.
PROGRAM :
//to find the small of three numbers using structur concept//
#include<stdio.h>
#include<conio.h>
struct number
{
int n1,n2,n3;
};
int main()
{
struct number s;
int small;
clrscr();
printf("Enter the three numbers:");
scanf("%d%d%d",&s.n1,&s.n2,&s.n3);
small=(s.n1<s.n2&&s.n1<s.n3)?s.n1:(s.n2<s.n3)?s.n2:s.n3;
printf("smallest of the three numbers=%d",small);
getch();
return 0;
}
12. What do you understand by union? How to access the members of the union? And
give the applications of it.
Ans.
UNIONS:-unions like structures contain members, whose individual data types may
vary .These is major distinction between them in terms of storage in structures each
member has its own storage location where as all the member of a union use the same
location. like structures a union can be declared using the keyword union is follows:
union item
{
int m;
float x;
char c;
}code;
Accessing elements of union:- Individual members can be used like other variables of the
same type there are two types of operations used for accessing members of a union.
1.Member operator (-)
Syntax: union_variable_name.member_name
Eg: stud2.name
2.Union pointer operator(->)
Syntax: union_varaible_name->member_name
Eg: p-->x
program:
#include <stdio.h>
Union point
{
int x;
int y;
};
int main()
{
Union point P;
P. x = 10;
printf("The x coordinate of p is %d\n", p.x);
P.y=20;
printf("The y coordinate of p is %d\n"p.y);
getch();
return 0;
}