Sunday, April 25, 2021

Fun with array pointers and function pointers

1-D and 2-D array pointers.   

// Pointer to an integer

int *p; 

// Pointer to an array of 5 integers

int (*ptr)[5];

// Pointer to an array of 5x2 

int (*ptr1)[5][2];

int arr[5];

int arr1[5][2];

    // Points to 0th element of the arr.

    p = arr;

    // Points to the whole array arr.

    ptr = &arr;

// Points to the while 5x2 array

ptr1 = &arry1;   

==========

Function Pointers


void fun(int a)

{

    printf("Value of a is %d\n", a);

}

  

int main()

{

    // fun_ptr is a pointer to function fun() 

    void (*fun_ptr)(int) = &fun;    

//  OR

void (*fun_ptr)(int) = fun;

  

    /* The above line is equivalent of following two

       void (*fun_ptr)(int);

       fun_ptr = &fun; 

    */

  

    // Invoking fun() using fun_ptr

    (*fun_ptr)(10);

//OR

fun_ptr(10);

    return 0;

}

No comments:

Post a Comment