-4

I know that pointers are used to store address of another memory location. The code is as follows:

    #include <stdio.h>
    main()
    {
      int a=10;
      void *ptr=10;
      printf("%u",ptr);
    }

My output was: 10

Can we store values in the pointers as in the above program? What is the main use of void pointer?

kalyan
  • 39
  • 2
  • 4
  • 1
    `void*` is often used for containers in C due to the lack of generics. – Jashaszun Aug 14 '14 at 17:23
  • By word "generic" Are you mean templates? – kalyan Aug 14 '14 at 17:24
  • @Jashaszun In that case, void pointer can process any datatyped variables. Am I right? – kalyan Aug 14 '14 at 17:27
  • 1
    @kalyan It can point to an object of any type. I'm not sure what you mean by "process". And no, it's not like templates. Templates in C++ have nothing to do with `void *` pointers, they work in a completely different (and safer) way. Seeing them as equivalent is wrong. – Filipe Gonçalves Aug 14 '14 at 17:30

5 Answers5

1

Can we store values in the pointers as in the above program?

The only values that should be assigned to pointer variable are:

  • addresses of other objects (such as int *foo = &x;);
  • addresses of functions (int (*fptr)(const char *, ...) = printf;);
  • addresses of well-defined system access points (such as a communications port or an interrupt register on an embedded system);
  • NULL (which represents a well-defined "nowhere").

You should not use pointer variables to store arbitrary values for arithmetic computation or display as you do in your example.

The declaration

void *ptr=10;

should have raised a diagnostic; you cannot directly assign an integer value to a pointer variable without a cast. You might want to raise the warning level on the compiler you're using.

The statement

printf("%u",ptr);

invokes undefined behavior, since the type of the argument (void *) doesn't match what the conversion specifier is expecting (unsigned int). It "worked" in the sense that it displayed the value you expected, but that's a matter of luck, not design. It could just as easily have printed out a garbage value.

What is the main use of void pointer?

It serves as a "generic" pointer type. Pointers to different types are not directly assignable to each other; IOW, you can't do something like

char *foo;
double *bar;

foo = bar; // bzzt

without explicitly casting bar to char *:

foo = (char *) bar;

Similarly, if you have a function that takes a pointer parameter of one type, you can't call it with a pointer of a different type:

void f(char *);
...
double x;
f(&x);       // bzzt

The one exception to this rule is objects of void * type; they can be assigned without needing a cast:

int x;
void *foo = &x;

and functions that take paramters of type void * can accept arguments of any object pointer type:

void f(void *);
...
double x;
int y;

f(&x);
f(&y);

They're used to implement limited forms of "generic" programming (where the algorithm is the same, but the types are different). The canonical example is the qsort library function, which can sort arrays of any type.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0
void *ptr=10;

This is not valid C, you need an explicit conversion with a cast like (void *) 10.

printf("%u",ptr);

This is not valid C, you need an explicit conversion with a cast like (unsigned int) ptr.

Regarding the use of void pointers, they are generic object pointers, so you can assign a pointer value of any object pointer type to a void *.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • I am using Turbo c compiler... My code was working perfectly without any type conversion. – kalyan Aug 14 '14 at 17:25
  • 1
    @kalyan You have to enable all your compiler warnings and fix them. – ouah Aug 14 '14 at 17:26
  • 1
    @ouah: The latest version of Turbo C is 24 years old; that might not be possible ;) – Oliver Charlesworth Aug 14 '14 at 17:27
  • @kaylan the fact that a compiler compiles your code does not mean that all compilers will accept your code – ouah Aug 14 '14 at 17:29
  • Kindly try this code in the online compiler: http://www.compileonline.com/compile_c_online.php – kalyan Aug 14 '14 at 17:29
  • I thought that: Here in the program, the address of value 10 is only stored in the *ptr. Whether my guess is right or wrong? – kalyan Aug 14 '14 at 17:33
  • It should be mentioned that the value a pointer stores is an address. Though `void *ptr = (void *)10` made be valid syntax, it's unlikely to be a valid address unless you're working on an embedded microcontroller. – Fiddling Bits Aug 14 '14 at 17:47
  • 1
    In modern C, `void *ptr = 10;` quite simply has no meaning. It is a *constraint violation* meaning that any conforming compiler must issue a diagnostic message. If that diagnostic is a warning rather than a fatal error, the standard says *nothing at all* about the behavior of the resulting program. A compiler that fails to complain about it is non-conforming; either apply options to cause your compiler to (attempt to) be conforming, or get a newer compiler. (It's *likely* that a compiler that accepts it will treat it like `void *ptr = (void*)10;`. – Keith Thompson Aug 14 '14 at 17:53
0

The type of the constant 10 is int; you are assigning an int to a void *. That's crazy, and wrong, there is absolutely no guarantee that you will get 10 when you print the value of ptr. You shouldn't do it. You don't know how pointer values are represented, and you don't have to. You don't care.

The use of void pointers is rather straightforward: it is just a way to deal with generic pointers. For example, you can implement a container of "something" by storing void pointers to the objects being stored. Be aware that you can't dereference it though - you need to cast it back to a valid pointer before doing so.

Also, on a side note, you should use %p to print pointers.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
0

Pointers are meant to store address (memory location) of a variable. So, their size is normally the word size of the OS (like 64bit = 8 byte in 64-bit OS).

By defining the type of pointer like int, char or void, we restrict the use of pointer for specific data type.

  1. By assigning a constant value to the pointer void *ptr=10;, you may have stored value but that's not the proper use pointer, and may cause segmentation fault later on. Always use pointer for storing address of variables like void *ptr = &a;.

  2. A void pointer doesn't define the type of data that it is pointing to. We can use void pointer to store address of variable without knowing its datatype.

Barun
  • 4,245
  • 4
  • 23
  • 29
0

No you cant directly store the value to the pointer unless and unless you refer it to something. Pointer assignment like x=y makes y point to the same pointee as x. YOu can consult this for more info Visit http://cslibrary.stanford.edu/106/