Expanded_Pointers_in_C12121212221212.pptx

KamranKiyani5 5 views 12 slides Sep 01, 2025
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

www


Slide Content

Pointers in C Principles of Computer Science

What is a Pointer? • A variable that stores the address of another variable. • Example: int *p;

Why Use Pointers? • Efficient memory usage • Allows dynamic data structures • Enables call by reference

Declaring and Initializing Pointers • int a=5; int *p=&a; • printf("%d", *p); // prints 5

Pointer Operators • &: Address-of operator • *: Dereference operator

Example Program #include <stdio.h> int main(){ int a=10; int *p=&a; printf("Value: %d", *p); return 0; }

Pointers and Arrays • Array name acts as a pointer. • Example: int A[5]; int *p=A; printf("%d", *(p+2));

Pointer Arithmetic • Increment/decrement pointers • Example: for(p=A;p<A+5;p++) printf("%d", *p);

Pointers and Functions • Call by Reference using pointers • Example: swap(&a,&b);

Dynamic Memory Allocation • malloc, calloc, realloc, free • Example: int *p=malloc(5*sizeof(int));

Pointers to Strings • char *str = "Hello"; • Pointer to first character of string.

Best Practices with Pointers • Always initialize pointers (use NULL) • Free memory after use • Avoid dangling pointers
Tags