1

I am reading some open-source C code and encountering A a = (A) b; type conversion many times. For example,

static void hexdump(const void* pv, int len)
{
  const unsigned char* p = (const unsigned char*) pv;
  // some other code
}

A a = (A) b; code happens mainly when b is a pointer, void * pointer most often. I have C++ background. I think in C++, the assignment operator takes= care of the type conversion automatically? Because it already know that a is of type A.

Is the explicit type conversion necessary in C?

Peng Zhang
  • 3,475
  • 4
  • 33
  • 41
  • 1
    Your "update" is in the wrong place. When you're getting answers that don't address the question, add comments below the answers asking for clarification, and then *fix your question.* Tacking on an update to your question does not provide notification to anyone; nobody will see your "update." – Robert Harvey Oct 16 '14 at 15:43
  • About your update: depends on the type of A and B. BTW pointer conversion and type conversion are different things (as a quick general rule: promotions to compatible types are implicit, everything else is not). What "promotion" and "compatible" mean (in various contexts) is a long and different story... – Adriano Repetti Oct 16 '14 at 15:49

6 Answers6

4

No, conversion from void* to char* in C (it's a common example to explain where they're different!) is implicit so casting is unnecessary (then wrong because it may hide a problem if you wrongly change char to int).

Quoting "The C Programming Language, 2nd edition" by K&R (§A.6.8):

Any pointer to an object may be converted to type void* without loss of information. If the result is converted back to the original pointer type, the original pointer is recovered. Unlike the pointer-to-pointer conversions discussed in Par.A.6.6, which generally require an explicit cast, pointers may be assigned to and from pointers of type void*, and may be compared with them.

Please note "If the result is converted back to the original pointer type" because is crucial: if instead of char* you had int* then it may be wrong because of memory alignment.

From C99 standard (§6.3.2.3) about when conversion is possible:

A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

Now let's see when can be implicit (thanks to mafso for very quick search), from C11 (n1570) §6.5.4p3:

Conversions that involve pointers, other than where permitted by the constraints of 6.5.16.1, shall be specified by means of an explicit cast.

Then §6.5.16.1:

One of the following shall hold: [...] the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) one operand is a pointer to an object type, and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
4

You've got it the other way around: the cast is absolutely necessary in C++, but it is not necessary in C.

This will compile in C, but not in C++:

static void hexdump(const void* pv, int len) {
  const unsigned char* p = pv;
  ...
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • In C++, if b is not a ``void *`` type, then we seldom do ``(A) b`` when we assign it to ``A a``. – Peng Zhang Oct 16 '14 at 15:34
  • 3
    @PengZhang Generally speaking, C++ wanted to make casting feel rather uncomfortable to programmers, while at the same time reducing the need for use of `void*`. Requiring an explicit cast of `void*` is part of the same philosophy. – Sergey Kalinichenko Oct 16 '14 at 15:38
2

It is not necessary in C but it helps silent compiler warnings for implicit casts in some cases.

In C++ however you need explicit casts to cast a void pointer into a pointer of a different type.

CliffordVienna
  • 7,995
  • 1
  • 37
  • 57
  • 1
    I've never got any warnings when implicitly assigning a `void *` to a pointer of any other type, however. – Dolda2000 Oct 16 '14 at 15:29
  • @Dolda2000 Which warning you will get depends on the compiler you are using. Some compilers with strong focus on C++ create warnings for code that is valid in C but not valid in C++. – CliffordVienna Oct 16 '14 at 15:32
1

In my practice, no casting at first, though there's compiler warnings, after testing pass, casting is added to eliminate warnings.

Peixu Zhu
  • 2,111
  • 1
  • 15
  • 13
0

In this case you are making a cast.

You are just saying for the compile. Hey, I'm sure that this is a type (A).

C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime

0

It's called a cast. It means that it converts b in the (A) type.

char c = 'a'; // c is a char
int b = (int) c // you tell your compiler to interpret c as an int
LiGhTx117
  • 218
  • 2
  • 8