-4

program 1:

int main()
{
    void v=8;
    printf("v=%d\n",v);
}

program 2:

int main()
{
    void *v=8;
    printf("*v=%u\n",*v);
    printf("v=%u\n",v);
}

compilation error on program 1:

**error**: variable or field ‘v’ declared void void v=0;

compilation errr on program 2:

**error**:invalid use of void expression printf("%d\n",*v);

Could anybody knows the behaviour of void and void* in the above program codes?

prashad
  • 107
  • 2
  • 15
  • 5
    What do you expect either of these programs to do? You can't just type random code and expect it to do something valid ;) – Oliver Charlesworth Nov 19 '14 at 08:26
  • You should know what's the purpose of `void`.. Your code doesn't make sense even for studying purposes. – Maroun Nov 19 '14 at 08:26
  • 3
    @prashad A compilation error is not the ‘output’ of a program. – Biffen Nov 19 '14 at 08:29
  • 1
    Since both programs have an error and even fail to compile, there is no behaviour in either of the programs. –  Nov 19 '14 at 08:29

2 Answers2

2

void has two uses:

  • Either as part of function declarations, stating that a function returns nothing, or takes no parameters.

  • Or as the generic pointer type void*, which can be used to convert to/from any other pointer to type, without an explicit cast.

C11 6.2.5/19 states that:

The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.

This means that you cannot define variable as void nor dereference a void*.

This is stated more clearly in 6.3.2.2

6.3.2.2 void

The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.)

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • No, that does not mean you cannot declare a variable as `void`. You indeed cannot, but that is only because of a special exception that says `void` is not allowed as a variable type. Similarly, that does not say you cannot reference `void*`. Variables in general can be declared (though not defined) with incomplete types, and pointers to incomplete types can be dereferenced. The easiest example of an incomplete type for which this is clearly verifiably true is by trying it with an array of unspecified length. –  Nov 19 '14 at 09:31
  • And my comment was incorrect too: I stated that there was an exception disallowing `void` as a variable declaration. That's not the case. Rather, there's an exception that renders such variable declarations completely and utterly useless, by disallowing the one operation that could potentially have some use: such a variable's address cannot be taken. –  Nov 19 '14 at 09:39
-1

A void * is just a pointer that Points to someting you don't know. So you can initialize ist. Even a void* isn't very useful, but it is a pointer and can be casted to a pointer of a specific type.

A void variable is "something you don't know" and so it can never be initialized and can not be declared.

xMRi
  • 14,982
  • 3
  • 26
  • 59