I try to practice generic functions writing this function:
void *scramble(void *arr, int ElemSize, int n, int *indArr);
But whenever I run the program with a debugger it crashes on the prototype line, using F11 (Step Into) I see the following:
1.
#else /* WPRFLAG */
__initenv = envp;
mainret = main(argc, argv, envp);
#endif /* WPRFLAG */
2.
if ( !managedapp )
exit(mainret);
And then the console message says that: The program '[8108] 1.exe: Native' has exited with code 0 (0x0).
This is my program (please ignore logical coding mistakes as I want to solve it on my own):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char BYTE;
void *scramble(void *arr, int ElemSize, int n, int *indArr);
void main()
{
int indArr[5]={5,4,3,2,1};
int numbers[5]={1,2,3,4,5};
char letters[5]={'a','b','c','d','e'};
int *newNum;
char *newLet;
newNum = (int*)scramble(numbers, sizeof(int), 5, indArr);
newLet = (char*)scramble(letters, sizeof(char), 5, indArr);
}
void *scramble(void *arr, int ElemSize, int n, int *indArr)
{
int i;
BYTE *read, *write;
void *res;
res = malloc(ElemSize*n);
write = (BYTE*)res;
read = (BYTE*)arr;
for (i = 0; i < n; i++)
{
memcpy(write + indArr[i]*ElemSize, read, ElemSize);
read += ElemSize;
}
return res;
}