C
09-Pointer

Pointers

What is a pointer?

  • A pointer is a variable that stores the address of another variable.
  • It is used to store the memory address of a variable.

Why use pointers?

  • To access the memory address of a variable.
  • To access the value of a variable indirectly.
  • To pass arguments by reference.
  • To return multiple values from a function.
  • To allocate memory dynamically.

Pointer Declaration

  • The declaration of a pointer is similar to the declaration of a variable.
  • The data type of a pointer must be the same as the data type of the variable it points to.
  • The pointer declaration is done by adding an asterisk (*) before the pointer name.
int *ptr;

Pointer Initialization

  • A pointer can be initialized with the address of a variable.
  • The address of a variable can be obtained by using the address-of operator (&).
int num = 10;
int *ptr = #

Pointer Dereferencing

  • Dereferencing a pointer means accessing the value of the variable it points to.
  • The value of a variable can be accessed by using the dereference operator (*).
int num = 10;
int *ptr = #
printf("%d", *ptr); // Output: 10

Pointer Arithmetic

  • Pointer arithmetic is used to perform arithmetic operations on pointers.
  • The arithmetic operations include addition, subtraction, increment, and decrement.
  • The size of the data type is taken into account when performing arithmetic operations.
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *ptr); // Output: 1
ptr++;
printf("%d", *ptr); // Output: 2

Pass-by-value

  • In C, arguments are passed to functions by value.
  • It means that a copy of the data is made and used inside the function, not affecting the original data.
void increment(int num){
  num++;
}
 
int num = 10;
increment(num);
printf("%d", num); // Output: 10

Pass-by-reference

  • Pointers can be passed as arguments to functions.
  • Passing a pointer as an argument allows the function to modify the value of the variable it points to.
void increment(int *ptr){
    (*ptr)++;
}
 
int num = 10;
increment(&num);
printf("%d", num); // Output: 11

Pointer to Array

  • A pointer to an array is a pointer that stores the address of the first element of an array.
  • It is used to access the elements of an array using pointer arithmetic.
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *ptr); // Output: 1
ptr++;
printf("%d", *ptr); // Output: 2

Pointer to Structure

  • A pointer to a structure is a pointer that stores the address of a structure.
  • It is used to access the members of a structure using the arrow operator (->).
struct Point{
    int x;
    int y;
};
 
struct Point p = {10, 20};
struct Point *ptr = &p;
printf("%d", ptr->x); // Output: 10
printf("%d", ptr->y); // Output: 20

Null Pointer

  • A null pointer is a pointer that does not point to any memory location.
  • It is used to indicate that the pointer is not pointing to a valid memory address.
  • It is represented by the constant NULL.
int *ptr = NULL;
if(ptr == NULL){
    printf("Null Pointer");
}