In this void* ic = b + j * sz;
and this void* jc = ic - sz;
lines ide writing that expression must be a pointer to a complete type. I need to wright function which can sort everything so i use void*. I don't now how to iterate with void*. How get access to elements void* base
like array.
UPD
pcharToInt is wrong. At fist i cast void*
to char*
and
turned out heart symbols. Than i tried to work with void*
.
#include <iostream>
using namespace std;
typedef int (*CFT) (const void*, const void*);
int pcharToInt(char* a);
int cmp1(const void* a, const void* b);
void insort(void* base, size_t n, size_t sz, CFT cmp);
int main() {
int arr[] = { 9, 3, 5, 3, 7, 9, 4 };
void* a = arr;
char* b = static_cast<char*>(a);
return 0;
}
int pcharToInt(char* a) {
int b = 0;
char* tmp = a;
while (*a) {
b *= 10;
b += (*a++ - '0');
}
a = tmp;
return b;
}
int cmp1(const void* a, const void* b) {
int n1 = 0;
int n2 = 0;
n1 = pcharToInt((char*)a);
n2 = pcharToInt((char*)b);
if (n1 > n2) return 1;
else return 0;
}
void insort(void* base, size_t n, size_t sz, CFT cmp) {
void* b = base;
for (int i = 1; i < n; i++)
{
for (int j = i; j > 0; j--)
{
void* ic = b + j * sz;
void* jc = ic - sz;
if (cmp1(jc, ic)) {
for (int k = 0; k < sz; k++) {
char tmp = jc[k];
jc[k] = ic[k];
ic[k] = tmp;
}
}
break;
}
}
}
UPD2 Its old code vitch char
char* b = static_cast<char*> (base);
for (int i = 1; i < n; i++)
{
for (int j = i; j > 0; j--)
{
char* ic = b + j * sz;
char* jc = ic - sz;
if (cmp1(jc, ic)) {
for (int k = 0; k < sz; k++) {
char tmp = jc[k];
jc[k] = ic[k];
ic[k] = tmp;
}
}
break;
}
}
UPD3
Trouble in line where I cast void*
to char*
. In result where are not right symbols. It must be numbers in example code.