Kaiyaha
Kaiyaha

Reputation: 480

Are different pointers considered to be different data types?

In order to have better understanding of pointers, I'd like to have this cleared up. Different data types require different pointers, like char* or int*. Are all these pointers considered to be different data types or are they just the same data type?

Upvotes: 3

Views: 502

Answers (4)

ryyker
ryyker

Reputation: 23208

When a char * is created, it points to an area of memory that will accommodate char sized granularity for its variables. A int * will point to an area of memory that will accommodate int sized granularity. So, although sizeof(int *) == sizeof(char *) (caveat) They have uniquely different properties.

For example, this becomes apparent when considering what happens when memory is created for each:

char *buf = malloc(10);//reserves 10 memory locations, sizeof(char) 
                       //(always 1 byte) wide

int *val = malloc(sizeof(*val)*10);// reserves 10 memory locations sizeof(int)
                                   // (typically 4 bytes wide for 32bit target) 

Note that in requesting memory allocation the count of bytes used in the malloc expression should always be a factor of the sizeof the type.

This illustrates, again, that although the size of the pointer variables will be the same (either 4 or 8 bytes, depending on either 32bit or 64bit addressing), the memory required to accommodate 10 elements of each type will be significantly different.

Upvotes: 2

Are different pointers considered to be different data types?

Yes.

Different data types require different pointers, like char* or int*. Are all these pointers considered to be different data types or are they just different types of the same data type?

All are pointers, but their type is different when they point to objects of different type.

Note that beside the difference of the type of the pointer; their size in memory can also be different:

Also there is nothing like "different types of the same data type". Either the type is different or it is the same.

Upvotes: 4

Eric Postpischil
Eric Postpischil

Reputation: 222362

Unless two types are the “same type,” they are different types:

  • int and long are different types even if they use exactly the same numbers of bits for the same things. We know this because C 2018 6.3.1.1 tells us they have different ranks (a hierarchy in rules about promotions and conversions).
  • struct foo { int x; } and struct bar { int x; } are different types even though they use exactly the same numbers of bits for the same things.
  • typedef defines a new name for an existing type; the new name is not a different type, just a different name for the same type.
  • char * and const char * are different types, as are int * and long *, again even if int and long are the same size.

Beyond that, C has a notion of “compatible” types. Roughly speaking, two types are compatible if one can be used in place of the other in certain situations. For example, an int [3] (array of three int) is compatible with an int [] (array of unspecified number of int) because an array of three int can supply a requirement for an array of an unspecified number of int. Filling in the size just completes the int [].

C 2018 6.7.6.1 tells us:

For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types.

From this, we can see that not only are two different pointers types are different types but that some pointer types are not compatible with other pointer types. The rules about compatibility are somewhat involved, since they need to reconcile various flexible or incomplete parts of types, such as functions without complete parameter lists.

C 2018 6.3.2.3 tells us about conversions between pointer types, including:

  • A pointer to an object type may be converted to void * and back.
  • A pointer to a non-qualified type may be converted to a pointer to a qualified types (such as char * to const char *).
  • A pointer to an object type may be converted to a pointer to another object type if it has the required alignment.
  • A pointer to a function type may be converted to a pointer to another function type.

Again, the fact that only certain conversions are defined indicates to us that different types of pointers are different, and possibly in fundamental ways that could prevent a C implementation from converting between them.

Upvotes: 3

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

Yes, they are different data types, as they point to different types of data.

In other words, their usage and properties varies (example: same pointer arithmetic on different pointer type will yield different result) and they have different alignment requirements.

Upvotes: 1

Related Questions