Header Ads

3.3 - Pointers

 Jargon
  • What's a Jargon?
– Jargon may refer to terminology used in a certain profession, such as computer jargon, or it may refer to any nonsensical language that is not understood by most people.
– Speech or writing having unusual or pretentious
vocabulary, convoluted phrasing, and vague meaning.
  • Pointer are perceived difficult
– Because of jargonification
  • So, let's dejargonify & understand them
Pointers
  • Just like a book analogy, Computers contains different different sections (Code) in the memory
  • All sections have different purposes
  • Every section has a address and we need to point to them whenever required
  • In fact everything (Instructions and Data) in a particular section has a address!!
  • So the pointer concept plays a big role here
Use of Pointers:
  • To have C as a low level language being a high level language
  • Returning more than one value from a function
  • To achieve the similar results as of ”pass by variable”
  • Parameter passing mechanism in function, by passing the reference 
  • To have the dynamic allocation mechanism
The 7 Rules of Pointer:
  • Rule 1 - Pointer is an Integer
  • Rule 2 - Referencing and De-referencing
  • Rule 3 - Pointing means Containing
  • Rule 4 - Pointer Type
  • Rule 5 - Pointer Arithmetic
  • Rule 6 - Pointing to Nothing
  • Rule 7 - Static vs Dynamic Allocation
Pointer - Multilevel
  • A pointer, pointing to another pointer which can be pointing to others pointers and so on is know as multilevel pointers.
  • We can have any level of pointers.
  • As the depth of the level increase we have to bit careful while dealing with it.
Example :

#include 
int main()
{
      int num = 10;
      int *ptr1 = #
      int **ptr2 = &ptr1;
      int ***ptr3 = &ptr2;
      printf( “%d” , ptr3);
      printf( “%d” , *ptr3);
      printf( “%d” , **ptr3);
      printf( “%d” , ***ptr3);
    return 0;
}


Pointers – Constant Pointer
  • A read only pointer
  • Valid address MUST be assigned while defining constant pointer
  • Once address assigned, it can't be changed
  • Pointers – 2D Array:

#include 
int main()
{
       int a[2][3] = {1, 2, 3, 4, 5, 6};
       return 0;
}
          "Total Memory: ROWS * COLS * sizeof(datatype) Bytes"
  •   Pointers – 2D Array - Referencing:

    • Pointers – 2D Array - Dereferencing:

      Example 1:
       
      Say a[0][1] is to be accessed, then decomposition happens like,:
      a[0][1] = *(a[0] + 1 * sizeof(type))
      A[0][1] = *(*(a + 0 * sizeof(1D array)) + 1 * sizeof(type))
      A[0][1] = *(*(1000 + 0 * 12) + 1 * 4)
      A[0][1] = *(*(1000 + 0) + 4)
      A[0][1] = *(*(1004))
      A[0][1] = 2

      Example 2:

    Say a[1][2] is to be accessed, then decomposition happens like,
    a[1][2] = *(a[1] + 2 * sizeof(type))
    A[0][1] = *(*(a + 1 * sizeof(1D array)) + 2 * sizeof(type))
    A[0][1] = *(*(1000 + 1 * 12) + 2 * 4)
    A[0][1] = *(*(1000 + 12) + 8)
    A[0][1] = *(*(1020))
    A[0][1] = 6
    

     

     

    ...

No comments