LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   void * pointer in function (https://www.linuxquestions.org/questions/programming-9/void-%2A-pointer-in-function-134770/)

xailer 01-14-2004 12:10 PM

void * pointer in function
 
hi

If I create the following function

void * fast(void *f) { -||- } ;

This way you can pass any type of argument to function and function can return any type . But things get tricky since I have to cast void * type to pointer to the type argument is of , or else I get an error .
So how do I check to what type pointer points to without raising exception ?

int i=1;
void *pi=&i;
printf("%d" , *(int *)pi ) ;

this prints value of i .Without the cast to (int *)I get an error

The following question may seem a bit silly , but I will ask anyways . Why must you cast void * type to whatever type pointer points to ,before you can you can extract the value of a variable pointed to --> ( *( int * )pi )

thank you

PS:I could use sizeof( *pi ) ,but that seems a bit unpractical

bye

jtshaw 01-14-2004 12:22 PM

Check out template functions and classes (the later if you use C++).

Also, sizeof isn't a good way to check what type you are using because the sizeof a type could possibly change with compiler revision, new architecture, ect.

jtshaw 01-14-2004 12:29 PM

Another thing, that function could die if you sent it anything other then an integer anyway because the printf statement specifically asks for an integer.

xailer 01-14-2004 12:30 PM

hi

My knowledge of C is kinda limited so...what is template function ?
Like a function blueprint of some kind ?

jtshaw 01-14-2004 12:36 PM

Not quite, in fact now that I am looking through one of my own books I am not sure template functions are possible in C. It has been a long time since I have writen anything other then a template class in C++.

You can, however, get the type of something using the typeof function which works the same way sizeof works. For instance, if you have a pointer called x you want to declare x to the typeof what x points to you could write:

type(x*) y;

Maybe this will help.

xailer 01-14-2004 01:02 PM

I dont think C has type() or typeof function

kev82 01-14-2004 01:57 PM

by xailer(post 1)
So how do I check to what type pointer points to without raising exception ?

you cant, when you cast to a void pointer you are saying the pointer has no type associated with it. so casting to a void pointer is generally quite a bad thing to do.

and C has no templates or rtti(run time type identification), that is C++

xailer 01-14-2004 02:26 PM

But how do some system function with void * type in their parameter list check what value you passed ?

kev82 01-14-2004 02:48 PM

can you give me an example as ive looked for functions that use void pointers but all the ones ive found only care about the pointer, not the value it points to

luxitan 01-14-2004 03:28 PM

this should work

Code:

enum pt_type
{
        _int,
        _char,
        _float
};

struct void_pt
{
  enum pt_type type;
  void* pt;
}

void* fc()
{
        struct void_pt* temp = malloc(sizeof(struct void_pt));
        temp->type=_int;
        temp->pt = (void*)&number;
        return temp;
}

i didn't tested it

itsme86 01-14-2004 03:48 PM

You can also do it without a struct so you're adding a "header" of sorts to your data:

Code:

enum { INT, CHR, FLT };

void *fc(void *ptr)
{
  char type = *(char *)ptr;
  int i;

  switch(type)
  {  // type-dependent code goes here
    case INT:
      i = *(int *)(ptr+1);
      *(int *)ptr = i*3.14;
  }

  return ptr;
}

int main(void)
{
  void *ptr = malloc(sizeof(int)+1);
  void *rv;
  int i = 1;

  *ptr = INT;
  *(int *)(ptr+1) = i;

  rv = fc(ptr);
  free(ptr);

  printf("PI * i = %d\n", *(int *)ptr);
}


deiussum 01-14-2004 04:15 PM

Dunno if it's what you want, but the OpenGL functions that use void* so that it can accept various types take an extra enum that tells the function what the data type is. (man glTexImage2D for example)

xailer 01-15-2004 02:06 PM

hi

Im not shure I completely understand your example itsme86 (Im still kinda week on C )

thank you all for helping me out

itsme86 01-15-2004 02:27 PM

Imagine the ptr as 2 parts. A 1-byte header and a data segment. If you wanted to pass a string to fc() it might look like this in memory:

1 h e l l o \0

The first byte is the type identifier (1 = CHR). An integer might look like this:

0 0 0 8 6 (0 = INT, 0086 = the 32-bit integer fc() should process).

So your switch() in the fc() function should test the first byte of the ptr to determine the data type and then have a case for each type.

Stick to the struct example shown by luxitan if you want though. The header thing can get messy if you're not 100% about what's going on :)

xailer 01-15-2004 04:17 PM

hi

this is what I find confusing

Quote:

Originally posted by itsme86


Code:

enum { INT, CHR, FLT };


int main(void)
{
  void *ptr = malloc(sizeof(int)+1);
  void *rv;
  int i = 1;

  *ptr = INT;
  *(int *)(ptr+1) = i;



I assume INT is really char and thus '*ptr=INT' saves value of 0 in the first byte .

But --> *(int*)(ptr+1)=1; pointer will point one element beyond where it currently does . But how many bytes apart from its current addresswill this address be will depend on the type of element pointer points to . But how did it know that it points to char value ( 1 byte ) when we didn't define pointer as pointer to char ?

thank you very much


All times are GMT -5. The time now is 03:28 PM.