void pointers and pointers to functions

Pls i need good explanations what void pointer is
And how pointers to functions work
Thanx

i need good explanations what void pointer is

http://www.learncpp.com/cpp-tutorial/613-void-pointers/


how pointers to functions work

http://www.learncpp.com/cpp-tutorial/78-function-pointers/
thats what i cant understand why it is a void function if it as type char or int
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// increaser
#include <iostream>
using namespace std;

void increase (void* data, int psize)
{
  if ( psize == sizeof(char) )
  { char* pchar; pchar=(char*)data; ++(*pchar); }
  else if (psize == sizeof(int) )
  { int* pint; pint=(int*)data; ++(*pint); }
}

int main ()
{
  char a = 'x';
  int b = 1602;
  increase (&a,sizeof(a));
  increase (&b,sizeof(b));
  cout << a << ", " << b << endl;
  return 0;
}
Last edited on
why it is a void function if it as type char or int

Not sure if I understand your question correctly.

The return type of a function has nothing to do with the type of any arguments.
In the case of increase (line 5), you're passing a void pointer. The function returns nothing, so it's a void function.

i am passing a& (memory address of char a) so it is has a type or b (address of type int)
void pointer supposed not to point to any type but we see clearly &a which is reference to
address of a which is TYPE char and b WITH TYPE int so its not exactly void pointer isnt it?
maybe im wrong in the all understanding of this concept please i need explanation
thanx in advance
Last edited on
Because increase says it takes a void point &a or &b is converted from a char pointer or an int pointer to a void pointer. This happens inside main.

Within increase, data is a void pointer. It no longer has type char * or int *.
i am passing a& (memory address of char a) so it is has a type or b (address of type int)
void pointer supposed not to point to any type but we see clearly &a which is reference to
address of a which is TYPE char and b WITH TYPE int so its not exactly void pointer isnt it?
maybe im wrong in the all understanding of this concept please i need explanation
thanx in advance


There are no special memory addresses for different types. Each address is the location of 1 byte of memory. A char is 1 byte, so it takes up 1 byte of memory. Char variables stored sequentially in memory, will also then just be stored in sequential memory each one byte and each addresses of the next char, increased in value by 1.

An int is 4 bytes, so it will take up 4 bytes, and so 4 memory addresses.

There is a special thing which is done for you by the compiler, which is to take into account the type. An example is pointer arithmetic.

In assembly, which C++ is compiled into, if you wanted to fetch a 4 byte value from memory, you give an instruction to get 4 bytes starting at the address you would consider to be it's address in C++, but you're really getting all the bits from 4 separate sequential memory addresses starting at that location.

So the compiler, knowing the type generates correct assembly for you.

C++ compilers make sure you are passing, as an argument, the correct type, as you have specified in your declaration. The type of the parameter must match the type of the argument. But if the parameter is a void pointer, then it is not associated with a type. It's type is void. You can cast a typed pointer as a void pointer, you should cast it first just to be explicit, or it will be implicitly casted to a void pointer. But when this happens, you lose all of the special actions from the compiler which implement the way your type information translates into assembly. So you need to cast it back to the type you wanted, otherwise the compiler does not have the information it needs to translate the code in a surely correct way according to what you may have intended.

So it's no longer pointing to any type, just to a 1 byte memory address in which you now only have information given that it is where the first byte (8 bits) or possibly the entire 8 bits of some value you are interested in are at.
Last edited on
Thanx alot htirwin and abstractionAnon
i undestand now how pointers to functions works but this code still confusing me i still cant read it
int main declare two variables m , n of type int minus is a pointer to function subtraction
i get confuse in the calling to function operation please help to understand whats going on from there

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <<n;
  return 0;
}
Last edited on
operation's third argument is a pointer to a function accepting two ints.
Line 21 declares a function pointer called minus and initializes it to the (address of) subtraction.

Lines 23 and 24 are standard function calls. Lines 23 passes a function pointer to addition. Line 24 passes the function pointer called minus which is an alias for subtraction. No magic so far.

At line 14, whatever function pointer was passed is called. Note that (*functocall) dereferences the pointer that was passed, effectively calling whatever function was passed on line 23 or 24.
Topic archived. No new replies allowed.