Trending September 2023 # Functions Pointers In C Programming With Examples # Suggested October 2023 # Top 11 Popular | Happystarlongbien.com

Trending September 2023 # Functions Pointers In C Programming With Examples # Suggested October 2023 # Top 11 Popular

You are reading the article Functions Pointers In C Programming With Examples updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Functions Pointers In C Programming With Examples

In this tutorial, you will learn-

Functions Pointers Example

For example, the next program swaps two values of two:

void swap (int *a, int *b); int main() { int m = 25; int n = 100; printf("m is %d, n is %dn", m, n); swap(&m, &n); printf("m is %d, n is %dn", m, n); return 0;} void swap (int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp;} }

Output:

m is 25, n is 100 m is 100, n is 25

The program swaps the actual variables values because the function accesses them by address using pointers. Here we will discuss the program process:

We declare the function responsible for swapping the two variable values, which takes two integer pointers as parameters and returns any value when it is called.

In the main function, we declare and initialize two integer variables (‘m’ and ‘n’) then we print their values respectively.

We call the swap() function by passing the address of the two variables as arguments using the ampersand symbol. After that, we print the new swapped values of variables.

Here we define the swap() function content which takes two integer variable addresses as parameters and declare a temporary integer variable used as a third storage box to save one of the value variables which will be put to the second variable.

Save the content of the first variable pointed by ‘a’ in the temporary variable.

Store the second variable pointed by b in the first variable pointed by a.

Update the second variable (pointed by b) by the value of the first variable saved in the temporary variable.

Functions with Array Parameters

In C, we cannot pass an array by value to a function. Whereas, an array name is a pointer (address), so we just pass an array name to a function which means to pass a pointer to the array.

For example, we consider the following program:

int add_array (int *a, int num_elements); int main() { int Tab[5] = {100, 220, 37, 16, 98}; printf("Total summation is %dn", add_array(Tab, 5)); return 0;} int add_array (int *p, int size) { int total = 0; int k; for (k = 0; k < size; k++) { total += p[k]; /* it is equivalent to total +=*p ;p++; */} return (total);}

Output:

Total summation is 471

Here, we will explain the program code with its details

We declare and define add_array() function which takes an array address( pointer) with its elements number as parameters and returns the total accumulated summation of these elements. The pointer is used to iterate the array elements (using the p[k] notation), and we accumulate the summation in a local variable which will be returned after iterating the entire element array.

We declare and initialize an integer array with five integer elements. We print the total summation by passing the array name (which acts as address) and array size to the add_array()called function as arguments.

Functions that Return an Array

In C, we can return a pointer to an array, as in the following program:

int * build_array(); int main() { int *a; a = build_array(); /* get first 5 even numbers */ for (k = 0; k < 5; k++) printf(“%dn”, a[k]); return 0;} int * build_array() { static int Tab[5]={1,2,3,4,5}; return (Tab);}

Output:

1 2 3 4 5

And here, we will discuss the program details

We define and declare a function which returns an array address containing an integer value and didn’t take any arguments.

We declare an integer pointer which receives the complete array built after the function is called and we print its contents by iterating the entire five element array.

Function Pointers

A pointer to function is declared with the * ,the general statement of its declaration is:

return_type (*function_name)(arguments)

After defining the function pointer, we have to assign it to a function. For example, the next program declares an ordinary function, defines a function pointer, assigns the function pointer to the ordinary function and after that calls the function through the pointer:

void Hi_function (int times); /* function */ int main() { void (*function_ptr)(int); /* function pointer Declaration */ function_ptr = Hi_function; /* pointer assignment */ function_ptr (3); /* function call */ return 0;} void Hi_function (int times) { int k; for (k = 0; k < times; k++) printf(“Hin”);}

Output:

Hi Hi Hi

We define and declare a standard function which prints a Hi text k times indicated by the parameter times when the function is called

We define a pointer function (with its special declaration) which takes an integer parameter and doesn’t return anything.

We initialize our pointer function with the Hi_function which means that the pointer points to the Hi_function().

Rather than the standard function calling by taping the function name with arguments, we call only the pointer function by passing the number 3 as arguments, and that’s it!

NOTE: It is not important to insert the address operator & and the indirection operator * during the function assignment and function call.

Array of Function Pointers

An array of function pointers can play a switch or an if statement role for making a decision, as in the next program:

int sum(int num1, int num2); int sub(int num1, int num2); int mult(int num1, int num2); int div(int num1, int num2);

int main() { int x, y, choice, result; int (*ope[4])(int, int); ope[0] = sum; ope[1] = sub; ope[2] = mult; ope[3] = div; printf(“Enter two integer numbers: “); scanf(“%d%d”, &x, &y); printf(“Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: “); scanf(“%d”, &choice); result = ope[choice](x, y); printf(“%d”, result); return 0;}

int sum(int x, int y) {return(x + y);} int sub(int x, int y) {return(x – y);} int mult(int x, int y) {return(x * y);} int div(int x, int y) {if (y != 0) return (x / y); else return 0;}

Enter two integer numbers: 13 48 Enter 0 to sum, 1 to subtract, 2 to multiply, or 3 to divide: 2 624

Here, we discuss the program details:

We declare and define four functions which take two integer arguments and return an integer value. These functions add, subtract, multiply and divide the two arguments regarding which function is being called by the user.

We declare 4 integers to handle operands, operation type, and result respectively. Also, we declare an array of four function pointer. Each function pointer of array element takes two integers parameters and returns an integer value.

We assign and initialize each array element with the function already declared. For example, the third element which is the third function pointer will point to multiplication operation function.

We seek operands and type of operation from the user typed with the keyboard.

We called the appropriate array element (Function pointer) with arguments, and we store the result generated by the appropriate function.

The statement result = ope[choice](x, y); runs the appropriate function according to the choice made by the user The two entered integers are the arguments passed to the function.

Functions Using void Pointers

For example:

void * cube (const void *);

Consider the following program:

void* cube (const void* num); int main() { int x, cube_int; x = 4; cube_int = cube (&x); printf(“%d cubed is %dn”, x, cube_int); return 0;}

void* cube (const void *num) { int result; result = (*(int *)num) * (*(int *)num) * (*(int *)num); return result;}

Result:

4 cubed is 64

Here, we will discuss the program details:

We define and declare a function that returns an integer value and takes an address of unchangeable variable without a specific data type. We calculate the cube value of the content variable (x) pointed by the num pointer, and as it is a void pointer, we have to type cast it to an integer data type using a specific notation (* datatype) pointer, and we return the cube value.

We declare the operand and the result variable. Also, we initialize our operand with value “4.”

We call the cube function by passing the operand address, and we handle the returning value in the result variable

Function Pointers as Arguments

In the stdlib.h header file, the Quicksort “qsort()” function uses this technique which is an algorithm dedicated to sort an array.

void qsort(void *base, size_t num, size_t width, int (*compare)(const void *, const void *))

void *base : void pointer to the array.

size_t num : The array element number.

size_t width The element size.

The following program sorts an integers array from small to big number using qsort() function:

int compare (const void *, const void *); int main() { int arr[5] = {52, 14, 50, 48, 13}; int num, width, i; num = sizeof(arr)/sizeof(arr[0]); width = sizeof(arr[0]); qsort((void *)arr, num, width, compare); for (i = 0; i < 5; i++) printf(“%d “, arr[ i ]); return 0;} int compare (const void *elem1, const void *elem2) { if ((*(int *)elem1) == (*(int *)elem2)) return 0; else if ((*(int *)elem1) < (*(int *)elem2)) return -1; else return 1;}

Result:

13 14 48 50 52

Here, we will discuss the program details:

We define and initialize an integer array The array size is stored in the num variable and the size of each array element is stored in width variable using sizeof() predefined C operator.

We call the qsort function and pass the array name, size, width, and comparison function defined previously by the user in order to sort our array in ascending chúng tôi comparison will be performed by taking in each iteration two array elements until the entire array will be sorted.

We print the array elements to be sure that our array is well sorted by iterating the entire array using for loop.

You're reading Functions Pointers In C Programming With Examples

Update the detailed information about Functions Pointers In C Programming With Examples on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!