Teaching Plan: Pointers (Basic → Intermediate)
Pointer Basics
Concepts:
- What is a pointer? (a variable that stores an address)
-
&(address-of) and*(dereference) operators - Declaring and using pointers
#include <stdio.h>
int main() {
int a = 10;
int *p = & a; // pointer to a
printf("Value of a: %d\\n", a);
printf("Address of a: %p\\n", & a);
printf("Pointer p stores address: %p\\n", p);
printf("Value at address stored in p: %d\\n", *p);
return 0;
}
int main() {
int a = 10;
int *p = & a; // pointer to a
printf("Value of a: %d\\n", a);
printf("Address of a: %p\\n", & a);
printf("Pointer p stores address: %p\\n", p);
printf("Value at address stored in p: %d\\n", *p);
return 0;
}
Practice:
- Print the address and value of different data types.
-
Change a variable's value using a pointer (
*p = 20;).
Pointer Arithmetic
Concepts:
- Incrementing/decrementing pointers
- Pointer movement depends on data type size
-
Difference between
p + 1for int, char, float, etc.
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d %d\\n", *p, *(p + 1)); // 10 20
p++;
printf("%d\\n", *p); // 20
return 0;
}
int main() {
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d %d\\n", *p, *(p + 1)); // 10 20
p++;
printf("%d\\n", *p); // 20
return 0;
}
Practice:
- Print array elements using pointer arithmetic (no indexing).
- Find the sum of an array using only pointers.
Pointers and Arrays
Concepts:
- Relationship between array name and pointer
- Passing arrays to functions
- Accessing array elements via pointers
#include <stdio.h>
void printArray(int *p, int n) {
for(int i = 0; i < n; i++)
printf("%d ", *(p + i));
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
printArray(arr, 5);
return 0;
}
void printArray(int *p, int n) {
for(int i = 0; i < n; i++)
printf("%d ", *(p + i));
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
printArray(arr, 5);
return 0;
}
Practice:
- Reverse an array using pointers.
- Find max/min element using pointers.
Pointers and Functions (Pass by Reference)
Concepts:
- Difference between call by value and call by reference
- How pointers enable modifying variables inside functions
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 5, b = 10;
swap(& a, &b);
printf("a = %d, b = %d\\n", a, b);
return 0;
}
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 5, b = 10;
swap(& a, &b);
printf("a = %d, b = %d\\n", a, b);
return 0;
}
Practice:
- Write a function to input two numbers and return their sum using pointers.
- Create a function to change multiple values in main via pointers.
Pointers to Pointers
Concepts:
- Pointer to another pointer
- Accessing value through multiple indirections
#include <stdio.h>
int main() {
int a = 100;
int *p = & a;
int **pp = & p;
printf("Value of a = %d\\n", a);
printf("Value via *p = %d\\n", *p);
printf("Value via **pp = %d\\n", **pp);
return 0;
}
int main() {
int a = 100;
int *p = & a;
int **pp = & p;
printf("Value of a = %d\\n", a);
printf("Value via *p = %d\\n", *p);
printf("Value via **pp = %d\\n", **pp);
return 0;
}
Practice:
-
Print addresses and values at each level (
a,p,pp). -
Modify
athrough**ppand observe the change.
Structure Pointers
Structures Recap
- A structure groups variables of different data types under one name.
- It helps organize related data together.
struct Student {
char name[20];
int age;
};
struct Student anil = {"Anil", 30};
printf("Name = %s, Age = %d", anil.name, anil.age);
char name[20];
int age;
};
struct Student anil = {"Anil", 30};
printf("Name = %s, Age = %d", anil.name, anil.age);
Note: The . (dot) operator is used to
access members like anil.name or anil.age.
What is a Pointer? (recap)
- A pointer stores the memory address of another variable.
-
&gives the address;*accesses the value from that address.
int x = 10;
int *p = & x;
printf("%d", *p); // prints 10
int *p = & x;
printf("%d", *p); // prints 10
Pointer to a Structure
- We can create pointers that store the address of a structure variable.
struct Student {
char name[20];
int age;
};
struct Student anil = {"Anil", 30};
struct Student *ptr;
ptr = & anil; // store address of structure variable
char name[20];
int age;
};
struct Student anil = {"Anil", 30};
struct Student *ptr;
ptr = & anil; // store address of structure variable
Accessing Members via Pointer
-
Wrong:
ptr.age = 25;(ptr is a pointer, not a variable) - Correct:
(*ptr).age = 25;
Note: Parentheses are important because the dot operator has higher priority.
(*ptr).age = 25; // correct
*ptr.age = 25; // wrong
*ptr.age = 25; // wrong
Arrow Operator (->)
-
The arrow operator is shorthand for
(*ptr).member. - It dereferences and accesses the member in one step.
ptr->age = 25; // same as (*ptr).age = 25
strcpy(ptr->name, "Anil");
strcpy(ptr->name, "Anil");
Example Program
#include <stdio.h>
struct Student {
char name[20];
int age;
};
int main() {
struct Student anil = {"Anil", 30};
struct Student *ptr = & anil;
printf("Before: %s is %d years old\\n", anil.name, anil.age);
(*ptr).age = 200;
ptr->age = 10;
printf("After: %s is %d years old\\n", ptr->name, ptr->age);
return 0;
}
struct Student {
char name[20];
int age;
};
int main() {
struct Student anil = {"Anil", 30};
struct Student *ptr = & anil;
printf("Before: %s is %d years old\\n", anil.name, anil.age);
(*ptr).age = 200;
ptr->age = 10;
printf("After: %s is %d years old\\n", ptr->name, ptr->age);
return 0;
}
Output:
Before: Anil is 30 years old
After: Anil is 10 years old
After: Anil is 10 years old
Key Points Summary
| Concept | Operator | Example | Meaning |
|---|---|---|---|
| Access via variable | . | anil.age | Access member directly |
| Get structure address | & | & anil | Assign to a pointer |
| Dereference pointer | * | (*ptr).age | Access structure via pointer |
| Access via pointer | -> | ptr->age | Shortcut for (*ptr).age |
When to Use Arrow Operator
- Used when working with structure pointers in:
- Linked Lists
- Trees
- Dynamic Memory Structures
struct Node {
int data;
struct Node *next;
};
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 5;
newNode->next = NULL;
int data;
struct Node *next;
};
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 5;
newNode->next = NULL;
Conclusion
.→ Used with structure variables.->→ Used with structure pointers.-
Both access structure members, but
->is simpler when using pointers.