0

Today one student came to me and asked me, sir we have int, float, char and all datatypes in C.

when we write int i, that means i is an variable of type integer and so on for float f and char c.

Similarly we have int *i means i is a pointer to an integer. same for float *f and char *c.

And we do have void* v; too in C.

void pointer is also called as generic pointer.

He asked me as we can have void pointer, similarly why can't we have void v (as a datatype)?

I was just speechless. So, I am requesting pls help me. How to make him understand.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122

1 Answers1

2

Here's how I would explain it:

In higher-level languages, variables represent abstract things, and it's up to the language to decide how to actually represent them in bits and push the bits around to act on those things. It makes sense to allow a variable to represent the concept of "nothing".

C is not like that. C variables are actual collections of bits stored in memory. What they represent is up to the programmer. It makes no sense for a C program to have a variable of type "nothing"--that would mean "allocate no bits of memory", and it wouldn't know what to do with that. In C, "int" means "allocate 32 bits of memory", "int *" means "allocate memory sufficient to hold a memory address, and when I dereference it, get 32 bits at a time". "Void *" means "allocate memory for a memory address, and I'll tell you later how many bits to fetch from there".

A "void" function return type in C means "return from the function, but don't pass any bits to the caller". In a higher-level language like Python, for example, "return None" means return to caller, and pass it some collection of bits that represents the abstract concept of 'nothing'", which is a different thing.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55