Important Points about Pointers in C Programming

  • Like any other variable, Pointers are also a variable which holds the address of another variable indicating that it is pointing  to that variable.
  • A pointer variable intPtrX always has address of another variable (pointer’s value) and *intPtrX has the value of the variable that it is pointing to. Address of the pointer variable is obtained by &intPtrX.

&intPtrX → address of pointer variable intPtrX, DB1221
intPtrX → address of the variable that it is pointing to, BD0023
*intPtrx → value of the variable it is pointing to, 50

  • One cannot assign value to the pointer variable directly as intPtrX = 35. This means changing the address that a pointer is pointing to. Correct way of assigning value is by load operation or by store operation.
int intX = *intPtrX; // load operation
*intPtrX = 35; //Store Operation
  • Once memory is allocated to the pointers in the program, it should be taken care to free that memory. Else that memory will be available to any other program and it will be always show as allocated to the program to which it is assigned. This type of memory loss to the program is called memory leak.
  • Understand the difference between the following pointers

*(intPtr +i) → increment the pointer address by ‘i’ and show the value at that address
*(intPtr++) → show the value at intPtr and increment the pointer address by 1
*(++intPtr) → first increment the pointer address by 1 and show the value at intPtr
*intPtr++ → show the value at intPtr and increment the pointer address by 1

  • In the world of array, intArray, &intArray and &intArray[0] are all refer to the same value. Although in plain English, these are said to be ‘read array’, ‘pointer to array’, and ‘pointer to the first element of array’,  in C, all three expressions mean the same thing.
  • Whenever we evaluate array elements, compiler decomposes it into pointer and then evaluates it.
int intArr[] = { 45, 67, 89 }; 
int *intArrPtr= &intArr[1]; // assigns pointer to 67
printf("%i\n", intArrPtr[1]); // although shown as array, it will be evaluated as pointers by incrementing the address as intArrPtr +1 and displays 89
  • Although array can be considered as single pointer, both are totally different concepts. Whenever an array is used, it is evaluated as pointers. Its elements are referred by incrementing the pointer. That mean if intArr is an array, then intArr [2] means *(intArr + 2). However evaluating an array as pointer by using *(intArr+2) is slower than evaluating it using a pointer (increment the pointer intArrPtr twice – intArrPtr++; intArrPtr++;). In other words, pointer increment is faster than array increment.
  • In the code, while(*intPtr != ‘\0’) can be replaced with while (*intPtr) for faster processing. This is because, (*intPtr!=’\0’) needs to be evaluated first and then it needs to be used in the while statement, whereas compiler evaluates while(*intPtr) directly.
  • Can we write intArr= intPtr? No. This is because intPtr is a pointer variable, whereas intArr is an array and is static. We cannot change the location of the first element of the array after it is declared. Hence we have to always assign array to a pointer variable, intPtr = intArr. We cannot declare intArr=intPtr

 

  • void fnExample(int *p) is same as void fnExample (int p[]), since arrays are always passed by reference which is equivalent to passing a pointer as argument.
  • Final word on pointer – always remember that pointer is also a variable but holds the address of another variable. When is confusion, always try to draw and try to understand what is it pointing to.
Translate »