Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void* will have the same representation and memory alignment as a pointer to char

I am reading a book on pointers named "understanding and using c pointers"

When it comes to void * it says

It has two interesting properties:

  1. A pointer to void will have the same representation and memory alignment as a pointer to char.

What am confused about is isn't the memory of all the pointers same? They why instead of writing void* is same as normal pointer it explicitly mentioned char pointers? Will really appreciate any help

like image 844
sara Avatar asked Mar 29 '17 11:03

sara


1 Answers

On most common architectures, pointer to any data type has the same representation, while pointer to function may differ. However, it's not a requirement, so it's possible to create valid C implementation, which uses different pointers for different data types. The reason behind this is that C standard tends to describe only crucial requirements, leaving a lot of freedom for possible implementations. Here is what standard says:

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

If you're curious to see examples of systems with different sizes for different data types, this question mentions these wonderful examples

like image 90
nnovich-OK Avatar answered Sep 28 '22 08:09

nnovich-OK