Author Topic: pointers in c  (Read 1122 times)

thrushitha

  • Full Member
  • ***
  • Posts: 26
pointers in c
« on: July 07, 2011, 04:56:14 am »
what does below line mean

void *ptr;







thanks in advance

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Re: pointers in c
« Reply #1 on: July 07, 2011, 11:54:23 am »
what does below line mean
void *ptr;

thanks in advance

It is a void pointer. Like char*, int*, float*, you can have a void*.
It is used mainly for casting one pointer to another and used in call back functions. The call back functions are usually a function pointer.

To know more about function pointer, click on http://www.softwareandfinance.com/CPP/Function_Pointers.html

Another typical example for function pointer is when using multithreading. The _beginthread function will take function pointer as an argument.

Refer to http://www.softwareandfinance.com/Visual_CPP/MultiThread.html


thrushitha

  • Full Member
  • ***
  • Posts: 26
Re: pointers in c
« Reply #2 on: July 08, 2011, 02:53:47 am »
still i didnt understand void *ptr, can u pls elobrate n explain






thanx in advance

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 69
Re: pointers in c
« Reply #3 on: July 10, 2011, 07:49:25 am »
still i didnt understand void *ptr, can u pls elobrate n explain
thanx in advance

I have given here the typecasting example. It is a pratical and working example. You can compile it in Turbo C and Visual C++ compiler. The call back function will accept 3 arguments:

1. Function Pointer named: cbfunc. The signature is two input arguments void*, void* and returns nothing,
2. Argument1 that will be used by the function cbfunc
3. Argument2 that will also be used by the function cbfunc

void MyCallBackFunc1(void*, void*) deals with int values;
void MyCallBackFunc2(void*, void*) deals with double values;

In thiis example see how int and double variables are going through void*. This is the use of void*.

#include <stdio.h>

void callbackfunc(void (*cbfunc)(void*, void*), void *arg1, void *arg2)
{
   cbfunc(arg1, arg2); // Function is getting called
}

void MyCallBackFunc1(void *arg1, void *arg2)
{
   int *p1 = (int*) arg1;
   int *p2 = (int*) arg2;
   printf("Int Values: %d %d\n", *p1, *p2);
}

void MyCallBackFunc2(void *arg1, void *arg2)
{
   double *p1 = (double*) arg1;
   double *p2 = (double*) arg2;
   printf("Double Values: %.2lf %.2lf\n", *p1, *p2);
}

void main()
{
   int ival1 = 10;
   int ival2 = 20;
   double dval1 = 12.34;
   double dval2 = 45.67;

   callbackfunc(MyCallBackFunc1, (void*)&ival1, (void*)&ival2);
   callbackfunc(MyCallBackFunc2, (void*)&dval1, (void*)&dval2);
}


Like us on http://facebook.softwareandfinance.com

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1