0% found this document useful (0 votes)
23 views12 pages

Unit - 4 Essay Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views12 pages

Unit - 4 Essay Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1. What is a pointer?

Explain declaration, initialization and accessing of a pointer


variable.
Ans.
Pointer : a pointer is a variable that holds the memory location of another variable stored in
computer memory . an address is the actual address of a value stored in the memory at the
address which the pointer points too.
Declaring a pointer variable : a pointer is declared in the same way like other variables
but it is denoted by "*"operator "*" is called as pointer operator which also means value at
the address operator
Syntax : data type space *variable name;
Example: int *p;
Accessing the elements of pointer :
The "&" is the address of operator and it is used to reference the memory address of a
variable . when we declare a variable three things happen
1. Computer memory is set a side for the variable .
2. The variable name is linked to that location in memory
3. The value of variable is placed into the memory that was set aside
Initializing a pointer : After declaration of a pointer it can be initialized with an address of
another variable
Syntax :datatype *pointer_variable_name variable_name ;
Pointer_variable= & variable;
Eg:
int x=10;
int *ptr ;
ptr =&x ;

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;
}

2. What is address arithmetic in C? Explain different arithmetic operations that can be


performed on
pointers.
Ans.
Address Arithmetic: Address arithmetic in C refers to the ability to perform arithmetic
operations that can be performed on pointers, which are variables that store memory
addresses of other variables .
Here are the different arithmetic operators that can be Performed on Pointers

Arithmetic operations on Pointers

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:

Generic Pointers (Void Pointers) :


•Declared using the `void*` keyword.
•Can store addresses of any data type (e.g., int, char, float, etc.).
•No data type is associated with the pointer, so the compiler doesn't perform any type
checking.
•Before dereferencing a void pointer, you must explicitly cast it to the correct data type.

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

Differentiating ptr++ and ++ptr :


Both `ptr++` and `++ptr` are used to increment a pointer, but they differ in their behavior:

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()
{

int arr[5] = {1, 2, 3, 4, 5};


int* ptr = arr;
printf("%d ", *++ptr); // Increments ptr, then prints 2
printf("%d ", *ptr); // Prints 2
return 0;
}
In summary, ptr++ returns the current value and increments the pointer afterward,
while ++ptr increments the pointer and returns the new value.

4. Give the syntax and explain arrays of pointers in detail.


Ans.
arrays of pointers:
Syntax: dataType *arrayName[arraySize];

- `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.

Initializing an Array of Pointers: You can initialize an array of pointers by assigning


addresses of variables to each element in the array:
Eg:
int var1 = 10, var2 = 20, var3 = 30;
int *ptrarray[3] = {&var1, &var2, &var3};

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 (`*`):

printf("%d %d %d", *ptrArray[0], *ptrarray[1], *ptrarray[2]);


Output: 10 20 30

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 *));

This code allocates memory for an array of 3 integer pointers.

Advantages and Applications


Arrays of pointers have several advantages and applications:

- They allow for efficient memory management and dynamic allocation.


- They enable the creation of complex data structures, such as linked lists and trees.
- They are useful in applications that require manipulating and processing large datasets.

In conclusion, arrays of pointers are a powerful tool in programming, offering


flexibility and efficiency in memory management and data processing.

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);

(d)free():-Deallocate the previously allocated space dynamically allocated memory created


with either calloc() or malloc() doesn't get freed on its own. you must explicity use free() to
release the space
Syntax:- free(p);
Eg: free(arr);
PROGRAM:
//to count sum of elements in an array //
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter the elements:");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter the enter the elements of array:");
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("sum=%d\n",sum);
free(ptr);
getch();
return 0;
}

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;
}

7. Explain the concepts of structure declaration, initialization, accessing the members


of structure.
Ans.
Structure Declaration : A structure is a collection of variables of different data types that
are stored together in memory. To declare a structure, you use the `struct` keyword followed
by the name of the structure and the variables that make up the structures
Declaration of structures :
SYNTAX : struct item {
data member1 ;
data member n ;
}
Eg:
struct Student
{
int rollNo;
char name[50];
float marks ;
};

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;
}

8. What is the advantage of structure? Differentiate it with union and arrays.


Ans.
Advantages of Structure :
1. Improved Code Organization: Structures allow you to group related variables together,
making your code more organized and easier to understand.
2. Better Memory Management: Structures enable you to manage memory more efficiently
by allocating memory for multiple variables at once.
3. Enhanced Data Hiding: Structures can be used to hide data from external interference,
promoting data encapsulation and abstraction.
4. Simplified Data Access: Structures provide a convenient way to access and manipulate
data using the dot (.) operator.

Differentiating Structures, Unions, and Arrays


Here's a comparison of structures, unions, and arrays:
Structure :
Definition :A collection of variables of different data types.
Memory Allocation : Each member gets separate memory allocation.
Data access : Members can be accessed independently.
Use cases :When you need to store and manipulate related data of different types.
Unions :
Definition : A collection of variables of different data types, but only one variable can be
used at a time.
Memory Allocation :All members share the same memory space.
Data Access : Only one member can be accessed at a time.
Use Cases : When you need to store different types of data in the same memory space.
Arrays :
Definition : A collection of variables of the same data type.
Memory Allocation : Each element gets separate memory allocation.
Data Access : Elements can be accessed independently.
Use Cases : When you need to store and manipulate a collection of data of the same type.

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;
}

You might also like