-1

I'm reading how pointers work here because I'm trying to understand qsort(). But the guide gives this code:

// 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;
}

without explaining what the line

pint=(int*)data;

and

pchar=(char*)data;

means. I understand what the rest means, but it doesbt quite make sense to me what (char*)data could mean. Is it pointing to the value of char? But how could that be if char is a variable type?

markovchain
  • 515
  • 1
  • 10
  • 25

7 Answers7

8

The line pint = (int*)data is doing two things

  1. The (int*)data is a conversion from void* to int*
  2. The pint = ... is an assignment of the value to pint

Ultimately what this function is attempting to do is increment the value pointed to by data by one value. It's a bit flawed though as a) the signature accepts any pointer type but the implementation only works with a fixed set of types and b) it incorrectly associates sizes with types.

Given that this is C++, a much better way of writing this would be to use a template. It accepts all input types which support ++.

template <class T>
void increase(T* pValue) {
  ++(*pValue);
}

Ideally though you'd just use a reference

template <class T>
void increase(T& value) {
  ++value;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2
(int*)data;

and

(char*)data;

are casts from one pointer type to another, in this case from void* to int* and char*. And after that the results are assigned to pint and pchar

Johnny Mnemonic
  • 3,822
  • 5
  • 21
  • 33
2

data is passed as a void*

(int*)data is casting it to a int*.

quandrei
  • 204
  • 1
  • 3
  • 10
1
pint=(int*)data;

and

pchar=(char*)data;

are casting these variables as pointers, just as you would cast a a double to an int, or vise versa.

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
1

pchar is a pointer to a char - this is what the declaration

char* pchar;

means. Thus, after the assignment

pchar=(char*)data;

the pointer pchar is pointing to the start of the block pointed to by data - and when you offset pchar by a number (say 5 - *(pchar+5)), it will point to a memory location that is "five chars" from the location pointed to by data. Since a char is one byte long, this means you are incrementing the location by 5. Since pint had been declared as int* pint, the same construction *(pint+5) will point to a location that is 5*sizeof(int) away... so the +5 means different things depending on how the pointer was declared!

In other words - the declaration tells the compiler how to treat the objects pointed to by the pointer, and what to do when you offset the pointer (increment, decrement, etc).

Floris
  • 45,857
  • 6
  • 70
  • 122
0

Pointer is not variable as any other. It does not hold regular value. What it does is just remembering some place in memory by holding it's address.

On any place in memory could start any variable of any type. By casting pointer into some other type than it was created for (like (char*) for example) you let the program know, that he should work with that place in memory as if it was variable of given type.

Kupto
  • 2,802
  • 2
  • 13
  • 16
0

A pointer points to a place in memory (in 32bit systems the width of ALL pointers is 32bit and 64bit systems is 64bit). When you write void* pointer you are telling the compiler that the data in the memory has no type, and therefore the compiler takes default width for to read the contents(eg. 8bit). Remember that the pointer itself is always 32/64 bit, but the data it points differs.

For example when you say char* pointer, it knows that the data is a character and read 2byte for it (32bit). It is the same for int* and others. Or for long* pointer it reads 4byte.

when you write pchar = (char*)data you are telling to the CPU (MMU actually) that the data stored at pchar address is a char and therefore when I said read that, read 2byte for me. When you said (short*) your are telling the MMU to read 1byte for you for that pointer.

if you use void*, the system will use its default size which I think is 1byte.

Ramyad
  • 107
  • 1
  • 8