Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print void type in pure C

i have a function like

void printMe (void *i)
{
    printf("%d", i);
}

where i want to pass a void pointer and print it to screen. The above example is fine if the i is integer, float or double but crashes if i is a char. There is no overloading in C like i usually use in C++. So the question is this, can we create a function in C that will print the element that is it's parameter, and if yes how is this possible because it totally eludes me at this moment.

like image 371
SMeyers Avatar asked Jan 27 '09 09:01

SMeyers


People also ask

What is void print in C?

The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

How do I print the contents of a void pointer?

To print the values stored in a void pointer, we can use the C-style casting. For Example : cout << *((int*)ptr); Here ptr is a void pointer that contains the address of an int variable.

What is const void * in C?

const void is a type which you can form a pointer to. It's similar to a normal void pointer, but conversions work differently. For example, a const int* cannot be implicitly converted to a void* , but it can be implicitly converted to a const void* .

What is void in C with example?

Void Functions The return type is "void" (all lower case). Void functions are mostly used in two classes of functions. The first is a function that prints information for the user to read. For example (for our purposes), the printf function is treated as a void function.


2 Answers

Q1: So the question is this, can we create a function in C that will print the element that is it's parameter

A: Not in the way you want. You will have to pass information to the function, telling it the type of data you're passing.

Q2: and if yes how is this possible because it totally eludes me at this moment.

A: It's eluding you because it can't be done. There is no metadata associated with a void* that the compiler or runtime can use to figure out they type it's pointing to. You need to either

  1. pass a structure that contains a
    pointer and information about what
    the pointer points to (e.g. an enum).
  2. pass an extra parameter with information about what the pointer points to

As the code stands the only thing you can print here is the address that i points to.

A void pointer points to raw data, printf assumes you know what data type you're printing, it has no intelligence and cannot "figure it out" for you.

It's that simple.

What you can do is pass type information to the function, but then you end up with something very like printf it's self, where you pass a formatting string containing type information about the data in the following arguements.

Hope this helps.

Also . . . "There is no overloading in C like i usually use in C++"

Even in c++ the overloading happens at compile time, and here there's no way for the compiler to know what data will be passed to that function, so even though you're used to overloading, it would never work like this (e.g. try this same thing using printf, but compile it with a C++ compiler, you'll get exactly the same results). Actually try

cout << i;

in the function above, and it will give you the address i points to, not the "value" of i. You'd need to cast i and derference it before you could get it's value

cout << *(int*)i;

So, to get the above working in C++ you'd need to have lots of overloaded functions (or a template function, which is really the same thing, except the compiler rolls the functions for you) e.g. overloaded functions

printMe(int i){...}
printMe(double d){...}
printMe(char c){...}
printMe(char* string){...}

In c you just need to give those functions specific names

printInt(int i){...}
printDouble(double d){...}
printChar(char c){...}
printString(char* string){...}
like image 170
7 revs Avatar answered Sep 22 '22 06:09

7 revs


For a start, you're printing the pointer, not what it points to. To print the actual contents, you need to pass *i to printf, not i.

If you really want to do this, one solution is:

void printMe (void *p, int typ) {
    switch(typ) {
        case TYP_INT: printf("%d", *((int*)p)); break;
        case TYP_CHR: printf("%c", *((char*)p)); break;
        /* and so on ... */
    }
}
like image 34
paxdiablo Avatar answered Sep 22 '22 06:09

paxdiablo