this is my code and i want to do this function to be general, how i do it?

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

void * DoIt (void *arr, int elemSize, int numElems, void (*f) (void *));
void Sqr (void *x);

void  main( )
{
    double a[3] = {1.0, 2.0, 3.0};

 DoIt (a,sizeof(double),3,Sqr);


}

void Sqr (void *x)
{
   *(double *)x =(*(double *)x) * (*(double *)x);
}


void * DoIt (void *arr, int elemSize, int numElems, void (*f) (void *))
{
	int i;	


for( i=0; i<numElems; i++)
{
if(i%2==0)
{
..........

}
}

the out put have to do the sqr function on the even numbers in array...

Here's a quickie example that might help:

#include <stdio.h>
#include <string.h>

void copy(void *a, void *b, size_t size, size_t n)
{
    unsigned char *pa = a;
    unsigned char *pb = b;

    while (--n < (size_t)-1) {
        memcpy(pb, pa, size);
        pa += size;
        pb += size;
    }
}

int main(void)
{
    int a[] = {1, 2, 3, 4, 5};
    int b[] = {0, 0, 0, 0, 0};
    int i;

    copy(a, b, sizeof *a, 5);

    for (i = 0; i < 5; i++)
        printf("%d\n", b[i]);

    return 0;
}

its still doesnt help..i just dont know,tried everthing

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

void * DoIt (void *arr, int elemSize, int numElems, void (*f) (void *));
void Sqr (void *x);

void  main( )
{
    double a[3] = {1.0, 2.0, 3.0};
	int i;

 DoIt (a,sizeof(double),3,Sqr);
	
	for (i = 0; i < 3; i++)
		printf("%d\n", a[i]);
	
}

void Sqr (void *x)
{
   *(double *)x =(*(double *)x) * (*(double *)x);
}


void * DoIt (void *arr, int elemSize, int numElems, void (*f) (void *))
{
	int i=0;
	unsigned char *st=arr;
	unsigned char *st1=arr;

		while(  i<numElems)
			{

			if(i%2==0)
				{

			//	memcpy(st,f(*st),elemSize);
				}
			i++;
			st+=elemSize;
		//	st1+=elemSize;
			}
}

At some point you'd need to call Sqr, you know. :icon_rolleyes:

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

void * DoIt (void *arr, int elemSize, int numElems, void (*f) (void *));
void Sqr (void *x);

void main( )
{
    double a[3] = {1.0, 2.0, 3.0};
    int i;

    DoIt (a,sizeof(double),3,Sqr);

    for (i = 0; i < 3; i++)
        printf("%f\n", a[i]);

}

void Sqr (void *x)
{
    double *px = (double*)x;

    *px *= *px;
}

void * DoIt (void *arr, int elemSize, int numElems, void (*f) (void *))
{
    unsigned char *parr = arr;
    int i;

    for (i = 0; i < numElems; i++, parr += elemSize)
    {
        if (i % 2 == 0)
            Sqr(parr);
    }
}

ok...i see,thank you very much..
i will learn from it :)

i could have done it with out the "unsigned char *parr = arr;", so why do i need it?

i could have done it with out the "unsigned char *parr = arr;"

Show me.

well..i can't do it without *parr,i stil not sure why i need it?

well..i can't do it without *parr,i stil not sure why i need it?

If you can't do it without parr, then clearly you need it. As for why, it's because void doesn't have a size, but char does. By casting void* to char*, you're essentially adding support for pointer arithmetic (necessary to access items in an array represented by a pointer) without changing the representation or alignment of the pointer.

I tried to do something a little bit different..

#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 = (int *)scrambleArr(a, 7, sizeof(int),func);

    for (i = 0; i < 7; i++)
        printf("%d", a[i]);
	printf("\n");

}

int func(void *x)\\tring to get the index of the char in arr 2.
{	
    int index[7]={4,2,0,5,1,6,3};
	int i;
	unsigned char *pt = x;
	unsigned char *pt1;
	int place;

	for(i=0;i<1;i++)
	{
		pt1=pt+index[i];
		place=pt1-pt;
		
	}	
		return place;	
	
}


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;

	for ( int i=0 ; i < n ; 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;

}
i want to get from function func(void*x) the place of the number/any other char in arry 2,..maybe you can help me with this one.
thank you

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.