if anybody can help me with this code...plz

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void * scrambleArr(void * arr, int numElem, int elemSize,int (*func)(void*));
int func(void *x);

void main( )
{
	int a[7]={1,2,3,4,5,6,7};	
    int i;
	int *p = NULL;


	 p = scrambleArr(a, 7, sizeof(int),func);

    for (i = 0; i < 7; i++)
        printf("%d", *p);
	printf("\n");

}

int func(void *x) //i want to return the new place of the char in new array...!?
{	
    int index[7]={4,2,0,5,1,6,3};
	int *p=index[0];

	
	return p;
}


void *scrambleArr(void *arr, int numElem, int elemSize,int (*func)(void*))
{
void* newArr = (void*) malloc(elemSize * numElem);
        unsigned char* new_ptr = newArr;
        unsigned char* orig_ptr =arr;
        char* orig_idx_ptr;
        int*    idx_ptr;
	int i;
	for ( i=0 ; i < numElem ; i++ )
        {
		         idx_ptr = func(orig_ptr) + i; // Get next entry from indArr
                orig_idx_ptr = orig_ptr +(*idx_ptr * elemSize); // find element in arr to copy
                memcpy(new_ptr, orig_idx_ptr, elemSize); // copy it to the new array
                new_ptr += elemSize; // move newArr to next element ready for next copy
         }

	return newArr;


}

Of course. The Psychic Programming InterNetwork will get right on it.

The problem is on line 36.

whats the problem,..?,,i didnt even finish the func(void*x) function...so afouse there will be a problem...thats why i am asking.
i dont know to finish the func function.
maybe you can help/?

16. p = scrambleArr(&a[0], 7, sizeof(int), &func); 27. int *p = &index[0];

If all func does is return an index, why do you need to pass a pointer to void? I recognize that you want to perform a random shuffle on a generic array, but you're overcomplicating things. Compare and contrast:

void shuffle(void *arr, size_t n, size_t size)
{
    unsigned char *temp = malloc(size);
    unsigned char *parr = arr;
    size_t i;

    for (i = 0; i < n - 1; i++) {
        unsigned char *a = parr + size * i;
        unsigned char *b = parr + size * (i + rand() % (n - i));

        memcpy(temp, a, size);
        memcpy(a, b, size);
        memcpy(b, temp, size);
    }

    free(temp);
}

i have to call the function func(void*x)..this is how i need it.
and i have to the the exect index where i want the chars from array 1 to go to.
for example:
array1: A,B,C,D
index arry: 4,2,1,3
array2: C,B,D,A
i should say the way i want the chars/anything else will be in array2.
thank you for your help!

Okay, it sounds like you have some specific requirements, but I'm having trouble understanding your summary. Can you post the requirements you were given instead of paraphrasing?

i have to use this function

void * scrambleArr(void * arr, int numElem, int elemSize,int (*func)(void*));

and func(void *x) gets the value of the char in arr, and returns the index of this value in new arry. for example arr1: A,B,C,D func gets the A and returns the index of the place where A should be located in Arry2.
scrambleArr wil copy A to its place in Arry2 for example the Func(void*x) returns 4, than A should be in index 4 in Arry2...and so on.

Since this thread still isn't solved, I'll post my previous solution. I think this code does what you wanted to do (it's very similar to your code).

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void* scrambleArr(void* arr, int numElem, int elemSize,int (*func)(int));
int func(int e);

int a[7]={1,2,3,4,5,6,7};	
int index[7]={4,2,0,5,1,6,3};

int main(int argc, char** argv)
{
	int i;
	int* p = NULL;

	p = scrambleArr(&a[0], 7, sizeof(int), &func);

	for (i = 0; i < 7; i++)
		printf("%d", p[i]);
	printf("\n");
	system("pause"); /* Remove if you don't want to pause. */

	return 0;
}

int func(int e)
{	
	return index[e];
}


void* scrambleArr(void* arr, int numElem, int elemSize, int (*func)(int))
{
	void* newArr = malloc(elemSize * numElem);
	char* new_ptr = newArr;
	char* orig_ptr = arr;

	int i;

	for(i = 0; i < numElem; i++)
	{
		orig_ptr += func(i) * elemSize;
		memcpy(new_ptr, orig_ptr, elemSize);
		new_ptr += elemSize;
		orig_ptr = arr;
	}

	return newArr;
}

interesting solution thanks,
but, i have to do it with "void main()"..and func(void*x)
i cant change it..

func(void*x) gets the index of Item in arry1 and returns the idex of this Item in arry2

i know it..correctly it should be int main, but in this case it must be void main..

i know it..correctly it should be int main, but in this case it must be void main..

What do you mean "it must be"? It shouldn't be by standards. Are you writing code for an embedded system? Or your own OS?

You're overcomplicating things. That void* argument (func) is unnecessary and so is the function pointer (argument of scrambleArr). You can pass the index array pointer to the scrableArr function (or make it global and constant, because it isn't altered in your code).

Is this an assignment?

Yes,i am a student and it was the expect orders, in oder to complicate things.

(*func(void*x) can point to a built in function built-in visual studio

(*func)(void*) can point to a built in function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.