Pointers - C P P

Theory


  • Pointers are variables that store the address of other variables
  • Dangling Pointer => A pointer pointing to a memory allocation where no value is stored
    • Either reuse it or Remove from stack
  • Symbol table
    • It is an important data structure created and maintained by compilers in order to store information about the occurrence of various entities such as variable names, function names, objects, classes, interfaces, etc
    • Content of symbol table cannot be changed
  • Memory address is stored in hexadecimal format
  • Size of "ptr" is normally 8 as it stores memory
  • Const Pointer
    • const dataType* var; => pointer variable point to a const value
    • dataType* const var; => const pointer variable point to the value, Memory address of pointer can't be changed
    • const dataType* const var; => const pointer pointing to a const variable
  • Benefits
    • Gives information about the type of data
    • Tells us about the amount of memory to consider
  • Type Casting
        int I = 65;
        char* p = (char*)&I;
    

Commands


  • dataType *ptr => Declare, Bad practice
  • dataType *ptr = &var => Initialize, ptr is a pointer to dataType
  • ptr = &var => Assigning memory address of variable in pointer, Referencing
  • ptr => Gives memory value stored
  • *ptr => Gives value in that memory address, Dereferencing
  • (*ptr)++ => Value increases by 1
  • ptr++ => Value increases by the size of its dataType
  • *var = ptr => Copying a pointer
  • void *ptr => Void Pointer
    • A pointer that has no associated data type with it
    • Cannot be dereferenced
    • Can hold address of any type and can be typecasted to any type
    • malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type in C
      • In C++, we must explicitly typecast return value of malloc to (int *)

Array Pointers


  • int var[N];
  • int *ptr = var => Initializing a pointer and pointing it to the first element of an array
  • var = &var[0] => Returns address of first memory block
  • *var = *ptr => It is equal to var[0]
  • *(var + n) = *(ptr + n) => It is equal to var[n]
    • n[arr] = *(n + var)
    • *var + 1 => Increase value of first element by 1
  • (var + n) = (ptr + n) => It is the address of the nth element of the array
    • ptr + 1 = ptr++ => Points to the next element of the array
    • var + 1 => Point to the next Index of the memory address, It is an Indexing pointer
    • var = var + 1 => Error, var is a constant pointer so its value can not be modified
  • sizeof(var) / sizeof(var[0]) => Gives the length of the array
  • Differences
    • sizeof(ptr) = 8 but sizeof(var) = 4 * number of int elements

Character Array


  • ch var[4] = "val";
    • Creates a temporary memory and stores the value then copies that memory in "var"
  • char *ptr = &ch[0]
  • cout << var; => Prints value and not the address as in case of integer array
    • cout << c; => Starts prints and stops at null pointer
  • char *ptr = "val"
    • Bad Practice, Creates a temporary memory and stores the value then stores the memory of first value in "ptr"

Functions


  • Passing a Pinter to a Function

        void func(int *p) {
            // Makes a copy pointer, CHanging its address won't affect original pointer
            // But changing its value will also affect the original
            cout << *p << endl;
        }
    
        int main() {
            int value = 5;
            int *p = &value;
            func(p);
        }
    
  • Passing Array

        void func(int arr[], int n) {
            // A pointer to the array is passed to "arr" here
            // Its size is 8 instead of 4 * number of elements
        }
        void func(int arr*, int n) {
            // Same as before
        }
    
        int main() {
            int arr[5] = {1, 2, 3, 4, 5};
            func(arr, 5);
            // Can also send a part of array
            func(arr + 3, 2);
        }
    
  • Return by Reference, Bad Practice, Local variable is being sent

        int* functionName(int var) {
            int* ptr = var;
            return ptr;
        }
    

Pointer to Pointer (Double Pointer)


  • Creating a double pointer
        int var = 5;
        int *ptr = &var;
        int **ptr2 = &ptr;
    
  • *ptr2 => Gives the memory address that ptr is pointing at
  • **ptr2 => Gives value at the memory that ptr is pointing at
Share: