Programming in C - Data structures

Data Structure

Teaching Plan: Pointers (Basic → Intermediate)

Pointer Basics

Concepts:

#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;
}

Practice:

Pointer Arithmetic

Concepts:

#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;
}

Practice:

Pointers and Arrays

Concepts:

#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;
}

Practice:

Pointers and Functions (Pass by Reference)

Concepts:

#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;
}

Practice:

Pointers to Pointers

Concepts:

#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;
}

Practice:

Structure Pointers

Structures Recap

struct Student {
  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)

int x = 10;
int *p = & x;
printf("%d", *p); // prints 10

Pointer to a Structure

struct Student {
  char name[20];
  int age;
};

struct Student anil = {"Anil", 30};
struct Student *ptr;
ptr = & anil; // store address of structure variable

Accessing Members via Pointer

Note: Parentheses are important because the dot operator has higher priority.

(*ptr).age = 25; // correct
*ptr.age = 25; // wrong

Arrow Operator (->)

ptr->age = 25; // same as (*ptr).age = 25
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;
}

Output:

Before: Anil is 30 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

struct Node {
  int data;
  struct Node *next;
};

struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 5;
newNode->next = NULL;

Conclusion