Skip to main content

Posts

Showing posts from October, 2013

Pointers and arrays in c

POINTERS AND ARRAYS: What is an array? An array is a data structure consisting of elements of similar data type.  The elements of an array are stored in consecutive memory locations. How to declare an array? Below is the template for array declaration. datatype  name[number of elements];           int   num[10]; Here, num is array of 10 integers. How to access array elements? Array indices are used to access array elements.  And the array index starts from zero.       int arr[5] = {1, 2, 3, 4, 5}; arr[0] gives us the element at the 0th index in the array arr .   Let us try to read the array elements one by one. arr[0] is 1  arr[1] is 2  arr[2] is 3  arr[3] is 4  arr[4] is 5  How to access array elements using pointers? To access array elements, assign the base address of array to the pointer.      int *ptr;      int arr[5] = {10, 20, 30, 40, 50};      p...

Function pointers

Pointer to function in c with examples: What is function pointer? Every function has an address.  We can assign the address of functions to pointers.  Then those pointers are called pointers to functions.  Pointers to function is also called as function pointers. How to declare function pointer? Below is the declaration of a function pointer.         int          (  *func_ptr  )             (int)   <return type> (* <pointer name> ) (argument type) Here, "func_ptr" is a pointer to a function whose return type is integer and it takes an integer argument. How to get the address of a function? Address of a function can be obtained from the function name.  The below program illustrates how to print/get address of a function.   #include <stdio.h>   int add(int a, int b) {         return (a+b);   }   int main() {     ...

constant pointer, pointer to a constant and constant pointer to a constant

const pointer, pointer to a constant and const pointer to a constant: If a pointer declaration contains const type qualifier, then it would be either const pointer or pointer to constant or constant pointer to a constant. const pointer: What is constant pointer? A constant pointer is a pointer whose value(address to which the pointer points to) cannot be changed after initialization.  But, we can change the value(pointee) to which the pointer points to. How to declare constant pointer? The qualifier const needs to be located in between the asterisk and pointer name.      int      * const ptr;     <type> * const <pointer-name> Below is an example program for constant pointers.   /* constant pointer */   #include <stdio.h>   int main() {         int num1 = 10, num2 = 20;         int * const ptr = &num1;         printf("ptr: 0x%x\t *ptr: %d\n", (in...

Pointer to an array vs Array of pointers

Difference between pointer to an array and array of pointers in c? Pointer to an array: Pointer to an array is also called as array pointer. How to declare pointer to an array? int (* ptr)[3] = NULL; /* pointer to an array of three integers */ The above declaration is the pointer to an array of 3 integers.  We need to use parenthesis to declare pointer to an array.  If we are not using parenthesis, then it would become array of pointers.  Here, the base type of pointer ptr is 3-integer array. How to assign value to array pointer(pointer to an array)? Consider the below example, int arr[3][3]; ptr = arr; "arr" is a two dimensional array with three rows and three columns.  We have assigned the address of arr[0][0](arr is equivalent to &arr[0][0]) to pointer ptr . So, pointer ptr points to the first row of the matrix "arr" .  If ptr is incremented(ptr++), then it will point to the next three elements in the array "arr" .  If ptr is decremented(ptr...