25.12.2020»»пятница

Pointers In Dev C++

25.12.2020

Pointers give greatly possibilities to 'C' functions which we are limited to return one value. With pointer parameters, our functions now can process actual data rather than a copy of data.

In order to modify the actual values of variables, the calling statement passes addresses to pointer parameters in a function.

A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable. Jul 08, 2010  Java Project Tutorial - Make Login and Register Form Step by Step Using NetBeans And MySQL Database - Duration: 3:43:32. 1BestCsharp blog Recommended for you. C Pointers vs Arrays. Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in many cases. For example, a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing. Function Pointer in C In C, like normal data pointers (int., char., etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

  1. Using smart pointers, we can make pointers to work in way that we don’t need to explicitly call delete. Smart pointer is a wrapper class over a pointer with operator like. and - overloaded. The objects of smart pointer class look like pointer, but can do many things that a normal pointer can’t like automatic destruction (yes, we don’t have to explicitly use delete), reference counting.
  2. C Pointers Creating Pointers. A pointer however, is a variable that stores the memory address as its value. Get Memory Address and Value. In the example above, we used the pointer variable to get the memory address. Modify the Pointer Value. You can also change the pointer's value.

In this tutorial, you will learn-

Functions Pointers Example

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

Output:

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

  1. 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.
  2. In the main function, we declare and initialize two integer variables ('m' and 'n') then we print their values respectively.
  3. 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.
  4. 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.
  5. Save the content of the first variable pointed by 'a' in the temporary variable.
  6. Store the second variable pointed by b in the first variable pointed by a.
  7. 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:

Output:

Here, we will explain the program code with its details

  1. 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.
  2. 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:

Output: Transfer data to android using 3utools.

And here, we will discuss the program details

  1. We define and declare a function which returns an array address containing an integer value and didn't take any arguments.
  2. 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.

Notice that a pointer, not an array, is defined to store the array address returned by the function. Also notice that when a local variable is being returned from a function, we have to declare it as static in the function.

Function Pointers

As we know by definition that pointers point to an address in any memory location, they can also point to at the beginning of executable code as functions in memory.

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

C++ Pointer In Class

You have to remember that the parentheses around (*function_name) are important because without them, the compiler will think the function_name is returning a pointer of return_type.

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:

Output:

  1. We define and declare a standard function which prints a Hi text k times indicated by the parameter times when the function is called
  2. We define a pointer function (with its special declaration) which takes an integer parameter and doesn't return anything.
  3. We initialize our pointer function with the Hi_function which means that the pointer points to the Hi_function().
  4. 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!

Keep in mind that the function name points to the beginning address of the executable code like an array name which points to its first element. Therefore, instructions like function_ptr = &Hi_function and (*funptr)(3) are correct.

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:

Here, we discuss the program details:

  1. 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.
  2. 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.
  3. 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.
  4. We seek operands and type of operation from the user typed with the keyboard.
  5. We called the appropriate array element (Function pointer) with arguments, and we store the result generated by the appropriate function.

C++ Pointer To Pointer

The instruction int (*ope[4])(int, int); defines the array of function pointers. Each array element must have the same parameters and return type.

Pointers In Dev C 2017

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

Void pointers are used during function declarations. We use a void * return type permits to return any type. If we assume that our parameters do not change when passing to a function, we declare it as const.

For example:

Consider the following program:

Result:

Here, we will discuss the program details:

  1. 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.
  2. We declare the operand and the result variable. Also, we initialize our operand with value '4.'
  3. We call the cube function by passing the operand address, and we handle the returning value in the result variable

Pointers In Dev C Download

Function Pointers as Arguments

Dev C++ 5.11

Another way to exploit a function pointer by passing it as an argument to another function sometimes called 'callback function' because the receiving function 'calls it back.'

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

  • void *base : void pointer to the array.
  • size_t num : The array element number.
  • size_t width The element size.
  • int (*compare (const void *, const void *) : function pointer composed of two arguments and returns 0 when the arguments have the same value, <0 when arg1 comes before arg2, and >0 when arg1 comes after arg2.

Pointer In Device Manager

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

Result:

Here, we will discuss the program details:

  1. We define compare function composed of two arguments and returns 0 when the arguments have the same value, <0 when arg1 comes before arg2, and >0 when arg1 comes after arg2.The parameters are a void pointers type casted to the appropriate array data type (integer)
  2. 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.
  3. 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 order.The comparison will be performed by taking in each iteration two array elements until the entire array will be sorted.
  4. We print the array elements to be sure that our array is well sorted by iterating the entire array using for loop.