0% found this document useful (0 votes)
16 views6 pages

Handouts Module 5 Pointers and Files Under Construction Vs

Uploaded by

udayampta
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)
16 views6 pages

Handouts Module 5 Pointers and Files Under Construction Vs

Uploaded by

udayampta
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/ 6

HANDOUTS 5

Introduction to Problem Solving through Programs


Module 5 (Pointers and Files):
Pointers and Addresses, Pointers and Function Arguments, Pointers and Arrays , Address Arithmetic,
Character Pointers and Functions. Pointer Arrays (Pointers to Pointers), Multi-dimensional Arrays,
Initialization of Pointer Arrays, Pointers vs. Multi-dimensional Arrays, Command-line Arguments, Pointers to
Functions.
File Operations. Accessing files: File handling functions (fseek(), ftell(), feof(), rewind(), fread(), fwrite()).
The C Preprocessor, File Inclusion, Macro Substitution, Conditional Inclusion.

POINTERS
Pointers are a fundamental feature of the C programming language, allowing for low-
level memory access and manipulation. A pointer is a variable that stores the memory
address of another variable, function, or even another pointer. This capability enables
dynamic memory allocation, efficient array handling, and the creation of complex data
structures like linked lists and trees.

Declaration and Initializationof Pointers

int var =10; // integer variable with value 10


int *ptr; // Declaration of an integer pointer (stores address of an integer variable)
ptr = &var; // Initialization (assigning address of integer variable ‘var’ to ptr)

Dereferencing
 Dereferencing a pointer means accessing the value stored at the memory address it points to.
 This is done using the asterisk (*) operator.
 int var = 10;
 int *ptr = &var; // Pointer initialized with the address of 'var'
 int value = *ptr; // Dereferencing pointer to get the value (value = 10)
Example:
Types of Pointers

Pointers in C can be classified into several types based on the data they point to:

Pointers Declaration
int *intPtr;
Integer Pointers: Point to integer values.
Array Pointers: Point to the first element of an array. char *arrPtr = array_name;

Structure Pointers: Point to structures. struct struct_name


*structPtr;

Function Pointers: Point to functions. int (*funcPtr)(int, char);


Double Pointers: Point to another pointer int **doublePtr;
NULL Pointers: Do not point to any memory location int *nullPtr = NULL;

Void Pointers: Can point to any data type. void *voidPtr;

Pointer Arithmetic
You can perform a limited number of arithmetic operations on pointers. These operations
are:
 Increment and decrement
 Addition and subtraction
 Comparison
 Assignment
 The increment (++) operator increases the value of a pointer by the size of the
data object the pointer refers to. For example, if the pointer refers to the second
element in an array, the ++ makes the pointer refer to the third element in the
array.
 You can compare two pointers with the following operators: ==, !=, <, >, <=,
and >=.
ARITHMATIC OPERATION INCREMENT Pointers in Array operations
(increment in pointer)

O/P: 1 2 3 4 5

Add two numbers using pointers Sum of an Array using pointers

INPUT 3
1
2
3
=6
Passing Pointers to Functions // Add two numbers using a function
(Pass by Reference) and pointer
 Pointers can be passed to functions
to modify the original data directly.
 This is an efficient way to avoid
passing large data structures.
1 #include<stdio.h>
int addition ();
int main ()
{
int result;
int (*ptr)();
ptr = &addition;
result = (*ptr)();
printf("The sum is %d",result);
}
int addition()
{
int a, b;
printf("Enter two numbers?");
scanf("%d %d",&a,&b);
return a+b;
}

Swap using pointers Pointer and Character


#include <stdio.h>
void swap(int*, int*); #include<stdio.h>
int main() { #include<string.h>
int a, b;
printf("Enter values for a and b:\n"); void main(){
scanf("%d %d", &a, &b);
printf("Values before swap: a= %d, b= //Declaring string and pointers//
%d\n",a,b); char *s="Sachin";
swap(&a,&b);
printf("\nValues after swap: a= %d, b= //Printing required O/p//
%d\n",a,b); printf("%s ",s);// Sachin//
return 0; }
}
void swap(int* x, int* y)
{
int t = *x;
*x = *y;
*y = t;
}
Null Pointer
 A null pointer is a pointer that does not point to any memory location and hence
does not hold the address of any variables.
 It essentially holds the value of a defined constant called NULL, indicating that it
doesn't currently point to any valid memory address.
 NULL is defined in the standard header file <stddef.h>.

 It's a good practice to initialize pointers to NULL when they are declared, especially
if they are not immediately assigned valid addresses.
 Initializing pointers with NULL helps in avoiding undefined behavior or
segmentation faults.
int *ptr = NULL;
 NULL is returned by functions that fail to allocate memory dynamically using
functions like malloc() or calloc().
 int *ptr = malloc(sizeof(int));
 if (ptr == NULL) {
 // Memory allocation failed
 }

#include <stdio.h>
#include <stdlib.h>
int main()
{ int* ptr;
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(5 * sizeof(int));
// Check if the memory has been successfully allocated or not
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0); }
else {
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < 5; i++) {
ptr[i] = i + 1; }
printf("The elements of the array are: ");
for (i = 0; i < 5; ++i) {
printf("%d, ", ptr[i]); }
}return 0;
}

Dangling Pointer
 A dangling pointer is a pointer that continues to point to a memory location that
has been deallocated.
 free(ptr);
 ptr = NULL; // Prevents 'ptr' from becoming a dangling pointer

You might also like