[go: up one dir, main page]

0% found this document useful (0 votes)
32 views30 pages

Unit 5

Uploaded by

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

Unit 5

Uploaded by

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

Introduction to Pointers

•A pointer is a variable that stores the memory address of another


variable.
•Rather than storing a direct value, it holds a reference to where
the value is located in memory.
•Pointers allow for efficient memory management and dynamic
memory allocation.
•They enable pass-by-reference in functions, which can modify
actual variables rather than working on copies.
•Pointers are essential for working with arrays and strings, and
they are heavily used in data structures like linked lists, trees, and
graphs.
Understanding Pointers
•A pointer is defined using an dereference operator (*)
before the pointer's name.

•Pointer Variable: A pointer stores the address of


another variable. It doesn’t directly hold the variable's
value but rather points to where the value is stored.
int a = 10; // normal variable
int *ptr; // pointer variable declaration
ptr = &a; // storing the address of a in ptr

Here, ptr contains the memory address of a.


Accessing the Address of a Variable

•The address-of operator (&) is used to get the address of


a variable.

•Syntax: &variable_name
int num = 50;
int *p;
p = # // p now stores the address of num
printf("%p", p); // prints the address of num

Here, the address of num is stored in p, and we can


print this address using %p.
Declaration and Initialization of Pointer Variables

•Pointers must be declared before use, specifying the type


of data they point to.

Declaration syntax:

•datatype *pointer_name;

•Eg:
int *p;
char *c;
Initialization: Assign a pointer to the address of an
existing variable.

int a = 20;
int *p = &a; // pointer p points to the address of a
Accessing a Variable through its Pointer

•To access or modify the value stored at the memory


address a pointer points to, you use the dereference
operator (*).

•Dereferencing: Access the value of the variable


that the pointer is pointing to.
int a = 10;
int *p = &a;
printf("%d", *p); // prints the value of a (10)
Chain of Pointers (Pointer to Pointer)

•A pointer can store the address of another pointer.


This is known as a pointer to pointer.

Syntax:
datatype **pointer_name;
int a = 10;
int *p = &a; // pointer to int
int **pp = &p; // pointer to pointer to int
printf("%d", **pp); // prints the value of a (10)
Pointer Expressions

•Pointers support arithmetic operations, primarily useful for


working with arrays.
Common operations include:
Pointer Addition/Subtraction: Adding/subtracting an
integer to a pointer moves the pointer by that many
elements in memory.

int arr[] = {10, 20, 30};


int *p = arr; // p points to the first element
p++; // p now points to the next element (20)
Pointer Comparison: Pointers can be compared to
check if they point to the same memory location.

if (p1 == p2) { ... } // checks if p1 and p2 point to


the same location
Pointer Increments and Scale Factor

•When you increment a pointer (p++), the pointer moves


to the next memory address based on the size of the data
type (called the scale factor).

•Example:
int a[3] = {1, 2, 3};
int *p = a;
p++; // moves to the next integer (p += sizeof(int))
•If p points to an integer, incrementing it increases
its value by the size of int (typically 4 bytes).

•For char, the increment is by 1 byte, for float, it’s


4 bytes, etc.
Array of Pointers

•An array of pointers is an array where each


element is a pointer.
•This is commonly used when working with strings
(array of character pointers).
•Helps in managing a list of strings or arrays more
efficiently.
char *arr[] = {"Hello", "World", "C Programming"};
printf("%s", arr[0]); // prints "Hello"

•Here, arr[] is an array of pointers, where each pointer


holds the address of the first character of each string.
Pointers as Function Arguments

Pass-by-Reference: When pointers are passed as


arguments, the function can modify the original
variables' values (pass-by-reference).
void increment(int *x) {
(*x)++; // increments the value of x
}
int main() {
int a = 10;
increment(&a); // passing the address of a
printf("%d", a); // prints 11
} void increment(int x) {
x++;
// increments the value of x
}
int main() {
int a = 10;
increment(a); // passing the address of a
printf("%d", a);
Functions Returning Pointers

•A function can return a pointer, usually pointing to a


local variable or dynamically allocated memory.

•Useful when the function needs to return an array or


a dynamically allocated memory location.
int* getArray() {
static int arr[] = {1, 2, 3};
return arr; // returning the address of the first element of arr
}
int main() {
int *p = getArray();
printf("%d", p[0]); // prints 1
}
Pointers to Functions

•A pointer to a function allows you to call a function


through a pointer.
•It can be passed as an argument to other functions.
•Used in callback functions, dynamic function calls, etc.

return_type (*function_pointer)(parameters);
int add(int a, int b) {
return a + b;
}
int main() {
int (*func_ptr)(int, int) = &add;
printf("%d", func_ptr(2, 3)); // calls the add function, prints 5
}
Pointers and Structures

•Pointers are often used to work with structures, especially


for dynamic memory allocation and linked data structures
(e.g., linked lists, trees).

Accessing Structure Members Using Pointers:


• You can use the -> operator to access members of a
structure through a pointer.
struct Student {
int id;
char name[20];
};
int main() {
struct Student s1 = {101, "John"};
struct Student *ptr = &s1;
printf("ID: %d, Name: %s", ptr->id, ptr-
>name); // access structure members using
pointer
}
File Management in C
•File Operations: C provides a set of standard
library functions for file handling (open, read,
write, close, etc.).
•File Types: C supports text and binary files.
•File Pointers: A file pointer (FILE *) is used to
keep track of the current position in the file
being read/written.
Common File Handling Functions:

•fopen(): Opens a file and returns a file pointer.

FILE *fp = fopen("file.txt", "r");

•fclose(): Closes an open file.

fclose(fp);
•fgetc(), fputc(): Reads and writes a character to/from a
file.

char ch = fgetc(fp); // read a character from file


fputc('A', fp); // write a character to file

•fprintf() and fscanf(): Formatted output/input to/from a


file.

fprintf(fp, "Hello World"); // writes a string to a file


fscanf(fp, "%d", &num); // reads a number from a file
fseek(): Moves the file pointer to a specific position.

fseek(fp, 0, SEEK_END); // move the pointer to the end of the file


THANK YOU

You might also like