Unit-4 Imp
Unit-4 Imp
Pointers
A pointer in C is a variable that stores the memory address of another variable.
Pointers allow you to indirectly access and manipulate the values stored in the memory
locations they point to.
Pointers are commonly used in C for dynamic memory allocation, array and structure
operations, and passing variables by reference to functions.
Pointer declaration
Pointer declaration defines a pointer variable. It specifies the data type and name of the pointer.
Pointer initialization
Pointer initialization is the process of assigning a memory address to a pointer variable.
Example program:
#include <stdio.h>
void main()
{
int a = 10;
int *ptr;
ptr = &a;
printf("Value of a: %d\n", a);
printf("Memory address of a: %p\n", &a);
printf("Value of ptr (memory address): %p\n", ptr);
printf("Value at ptr (value of a): %d\n", *ptr);
}
Q) Briefly describe about pointer arithmetic in C?
Pointer arithmetic
Pointer arithmetic allows user to manipulate memory addresses by performing arithmetic
operations on pointers.
The value of the pointer changes based on the size of the data type it points to.
Importance of pointer arithmetic
Pointer arithmetic improves memory access speed.
It helps manage dynamic memory allocation.
It's necessary for low-level system programming.
Decrement a Pointer
User can use -- to move a pointer to the previous element.
Example:
int arr[5] = {10, 20, 30, 40, 50};
int *p = &arr[2]; // Points to arr[2] (value 30)
p--; // Moves to arr[1] (value 20)
Compare Pointers
User can compare pointers using comparison operators (==, !=, <, >, <=, >=) if they point
to elements of the same array.
Example:
int arr[5] = {10, 20, 30, 40, 50};
int *p1 = &arr[1];
int *p2 = &arr[3];
if (p1 < p2)
{
printf("p1 points to an earlier element.\n");
}
Program:
#include <stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
// Possible operations
printf("Possible operations:\n");
printf("ptr1 + 1 = %p\n", ptr1 + 1);
printf("ptr1 - 1 = %p\n", ptr1 - 1);
printf("ptr2 - ptr1 = %d\n", ptr2 - ptr1); // Output: 2
printf("ptr1 < ptr2 = %d\n", ptr1 < ptr2); // Output: 1 (true)
return 0;
}
Q) Explain about dynamic memory allocation functions with example
programs.
Example program
Example program
c) realloc() function:
• It is used to resize dynamically allocated memory blocks.
• It creates mew memory and transfers old data into newly created memory space.
• If realloc() is not successful to create memory, then user loses previous data.
• Syntax: pointer_variable = (datatype *) realloc (old_pointer, new_memory_size);
• Example: new_ptr=(char *)realloc(old_ptr, 20);
Example program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char *p,*q;
p=(char *)malloc(10); // old memory size
printf("Enter your name: ");
scanf("%s",p);
printf("%s \n",p);
q=(char *)realloc(p,20); // new memory size
printf("Enter new string: ");
scanf("%s",q);
printf("%s",q);
}
d) free() function:
This function is used to deallocate memory which is allocated dynamically. This function
takes only one parameter that is also a pointer.
Syntax: free(pointer_variable);
Example program
#include <stdio.h>
#include <stdlib.h>
void main()
{
int *ptr=(int *)malloc(sizeof(int));
*ptr = 15;
free(ptr);
printf("%d", *ptr);
}
Q) Discuss briefly about command line arguments in C.
When command line arguments are passed main() method receives them with the help of two
formal parameters and they are,
int argc
char** argv[ ]
int argc - It is an integer argument used to store the count of command line arguments are passed
from the command line.
char** argv[ ] - It is a character pointer array used to store the actual values of command line
arguments are passed from the command line.
User must make sure that in command line argument, argv[0] stores the name of the program or
name of the file, similarly argv[1] gets the pointer to the first command line argument that has
been supplied by the user, and *argv[n] denotes the last argument of the list.
Example Program:
Structure
A structure is a collection of elements of different data types.
A structure is also called a user-defined data type.
All elements of a structure are stored at contiguous memory locations.
A variable of structure type can store multiple data items of different data types under a
single name.
For example, the data of an employee in a company, such as name, employee ID, salary,
address, and phone number, can be stored in a structure data type.
Syntax:
struct <tag_name>
{
data_type1 var1,var2,var3,...;
data_type2 var1,var2,var3,...;
……..
data_typeN var1,var2,var3,....;
};
Example:
struct student
{
char name[10];
int rollno;
float marks;
};
A structure can be defined using the keyword "struct".
The tag-name specifies the name of the structure.
Defining a structure without a structure variable is known as a structure template, but its
size is not always 1 byte. The size of a structure template depends on the sizes of its
members.
Syntax:
struct <tag_name>
{
datatype1 var1,var2,..;
datatype2 var1,var2,..;
..........
datatypeN var1,var2,..;
};
struct <tag_name> structure_variable_name;
Example:
struct student
{
char name[10];
int rollno;
float marks;
};
struct student s1;
Syntax:
struct <tag_name>
{
datatype1 var1,var2,..;
datatype2 var1,var2,..;
..........
datatype3 var1,var2,..;
};
struct <tag_name> structure_variable_name={val1,valu2,...};
Example:
struct student
{
char name[10];
int rollno;
float marks;
};
struct student s1={“Ananth”,123,97.45};
Example Program
// Simple example program on structures.
#include <stdio.h>
struct student
{
char name[30];
int rollno;
float marks;
};
void main()
{
struct student s1={"Ananth",123,97.75};
printf("Name: %s\n", s1.name);
printf("Roll no: %d\n", s1.rollno);
printf("Marks: %f\n", s1.marks);
}
Q) Explain about array of structures with example program in C?
Array of structures
An array of structures is a collection where each item (structure variable) is a structure, used
to store related information together.
Structures group different types of data under one name, making it easy to organize.
All items (structure variables) in the array follow the same structure, keeping the data
consistent.
You can access each structure in the array using an index, like with any array.
The fields (parts) of a structure can be accessed using the dot (.) operator.
Arrays of structures help organize and manage multiple sets of related information.
They are often used for things like student records, employee details, or product lists.
Syntax:
struct StructureName
{
data_type field1;
data_type field2;
};
struct StructureName arrayName[arraySize]; // Declare an array of the structure
Example Program:
// Program to implement array of structures.
#include<stdio.h>
void main()
{
struct Student
{
char name[10];
int rollno;
float marks;
};
int n,i;
printf("How many students details you want: ");
scanf("%d",&n);
struct Student s[n]; // Arry of structures
for(i=0;i<n;i++)
{
printf("Enter student-%d details: ",i+1);
scanf("%s%d%f",s[i].name,&s[i].rollno,&s[i].marks);
}
printf("All students details are: \n");
for(i=0;i<n;i++)
{
printf("Name:%s ",s[i].name);
printf("Roll Number:%d ",s[i].rollno);
printf("Marks:%.2f \n",s[i].marks);
}
}
The size of a structure is the sum of sizes of The size of a union is the size of its largest
its members. member.
All members store their values at a time. Only one member can store value at a time.
All members can be accessed at the same Only the last assigned member can be
time. accessed.
Suitable for grouping related data of different Suitable for saving memory when only one
types. member is needed at a time.
It takes more memory as compared to union. It takes less memory as compared to structure.
struct student union student
{ {
char grade; char grade;
float marks; float marks;
}; };
Here, size of structure is 5 bytes. Here, size of union is 4 bytes.