Hi everyone,
I just got a problem referencing void pointer. If in my code, I replace *rbuf= &svar by (rbuf= &svar;); it does not complain but it does not return the real value (rbuf is a void pointer). Please Can anyone give me any advise. I would really apreciated

/*****************************************************/
#include <stdio.h>
#include <math.h>


int chamaleon (void *sbuf, void *rbuf, int count);


int main()

{ 
  const int max = 512 * 512;
  double sbuf[max];
  double rbuf[max];
  double rr;
  int count=max;
  int i;

  for (i = 0; i < count; i++)

    sbuf[i] = (i + 1) * 1.0;



      chamaleon (sbuf, rbuf, count);

      printf ("Value of rbuf outside function is =  %g\n", *((double *)rbuf));

  return 0;

}

int chamaleon (void *sbuf, void *rbuf, int count)
{
     double svar = 0.0;
     int i;
     for(i=0;i < count;++i) 
      svar = svar + *((double *)sbuf + i);

     *rbuf= &svar; /***  here is the problem (rbuf= &svar;) */

     printf ("Value of rbuf inside function is =  %g\n", svar);
      return(0);

}

Its an error because you can't dereference a void pointer..rbuf is a void pointer.

*rbuf= &svar;

What exactly are you trying to a accomplish here? What is the purpose of the program?

Hi Gerard4143,
Thanks for your reply. I'm trying to create a general function that will collect the information from other processes (I have multi-processes; Yeah MPI) this function receive the information form other processes and make an operation (like here SUM) and send back the result to other process and print it.

I check the sending and receiving part and they are working properly but when I want to return the value, I have problems.

I guess we can deference a void pinters;for example the following MPI command is similar to what I wanna do;

int MPI_Reduce ( void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm )

*sendbuf is input. And *recvbuf is output (void pointer) it return the address of the variable ( that can be int, float, double, etc)

Basically I am doing something similar. Thanks again for your help.

Its an error because you can't dereference a void pointer..rbuf is a void pointer.

*rbuf= &svar;

What exactly are you trying to a accomplish here? What is the purpose of the program?

Hi Gerard4143,
Thanks for your reply. I'm trying to create a general function that will collect the information from other processes (I have multi-processes; Yeah MPI) this function receive the information form other processes and make an operation (like here SUM) and send back the result to other process and print it.

I check the sending and receiving part and they are working properly but when I want to return the value, I have problems.

I guess we can deference a void pinters;for example the following MPI command is similar to what I wanna do;

int MPI_Reduce ( void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm )

*sendbuf is input. And *recvbuf is output (void pointer) it return the address of the variable ( that can be int, float, double, etc)

Basically I am doing something similar. Thanks again for your help.

I meant what are you trying to do in the posted code...Its my impression that your trying to pass the addresses of two arrays and then manipulate them inside said function....Note I cast the void pointers to double pointers.
Like so

testit.c

#include <stdio.h>
#include <math.h>

#define MAX 512

void chamaleon (void *sbuf, void *rbuf, int count);

int main()
{
	double sbuf[MAX];
	double rbuf[MAX];
	
	int i;

	for (i = 0; i < MAX; i++)
		sbuf[i] = (i + 1) * 1.0;

	chamaleon (sbuf, rbuf, MAX);

	for (i = 0; i < MAX; ++i)
		fprintf(stdout, "rbuf[%i]->%f\n", i, rbuf[i]);

	return 0;

}

void chamaleon (void *sbuf, void *rbuf, int count)
{
	int i = 0;
	double *s = (double*)sbuf;
	double *r = (double*)rbuf;

	for(i = 0; i < count; ++i)
	{
		*r = *s;
		++r;
		++s;
	}
}

Thanks Gerard4143, This work very well in my code. Thanks so much.

I meant what are you trying to do in the posted code...Its my impression that your trying to pass the addresses of two arrays and then manipulate them inside said function....Note I cast the void pointers to double pointers.
Like so

testit.c

#include <stdio.h>
#include <math.h>

#define MAX 512

void chamaleon (void *sbuf, void *rbuf, int count);

int main()
{
	double sbuf[MAX];
	double rbuf[MAX];
	
	int i;

	for (i = 0; i < MAX; i++)
		sbuf[i] = (i + 1) * 1.0;

	chamaleon (sbuf, rbuf, MAX);

	for (i = 0; i < MAX; ++i)
		fprintf(stdout, "rbuf[%i]->%f\n", i, rbuf[i]);

	return 0;

}

void chamaleon (void *sbuf, void *rbuf, int count)
{
	int i = 0;
	double *s = (double*)sbuf;
	double *r = (double*)rbuf;

	for(i = 0; i < count; ++i)
	{
		*r = *s;
		++r;
		++s;
	}
}
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.