Why Is the Data Type Needed in Pointer Declarations

Why is the data type needed in pointer declarations?

The data type is needed when dereferencing the pointer so it knows how much data it should read. For example dereferencing a char pointer should read the next byte from the address it is pointing to while an int pointer should read 2 bytes.

Why to specify a pointer type?

Type of a pointer is needed in following situations

  • Dereferencing the pointer
  • Pointer arithmetic

Here is the example of dereferencing the pointer.

{
char *ptr; //pointer of type char
short j=256;
ptr=&j; // Obviously You have to ignore the warnings
printf("%d",*ptr)
}

Now because ptr is of type char so it will only read one byte. But the binary value of 256 is 0000000100000000 but because ptr is of type char so it will read only first byte hence the output will be 0.

Note: if we assign j=127 then output will be 127 because 127 will be hold by first byte.

Now, the example of pointer arithmetic:

{
int *ptr;
int var=0;
ptr=&var;
var++;
ptr++;// pointer arithmetic
}

Are statements var++ and ptr++ are same thing? No, var++ means var=var+1 and ptr++ means ptr=ptr+4. Because the compiler "knows" this is a pointer and that it points to an int, it adds 4 to ptr instead of 1, so the pointer "points to" the next integer.

Why the datatype of the pointer should be same as the datatype of the variable to which it is addressing?

The datatype only helps in allocating the required memory size to the specified datatype.

This is not true. The type of a pointer p also tells the compiler what type to use for the expression *p.

If p is an int *, then *p has type int, and, if the program uses an expression such as a *p + 3, the compiler will generate an integer add instruction (or equivalent code). If p is a float *, then *p has type float, and, if the program uses an expression such as *p + 3, the compiler will generate a floating-point add instruction (or equivalent code). These different instructions will cause the computer to treat the bits of *p differently.

When it comes to the address of the memory, irrespective of the datatype of the variable, both addresses will be in the same format.

This is often true in C implementations but is not always true. The C standard allows pointers of different types to have different representations, with certain exceptions. (Pointers to character types and to void must have the same representation as each other. Pointers to structure types must have the same representation as each other. Pointers to union types must have the same representation as each other.)

In C++, why is mentioning the type of the pointer necessary?

If you declare b as auto, the compiler will use type inference and determine the type itself. But if you use a specific type, it has to be the correct one.

Why can't we generalize the pointer declaration?

TL;DR because, different data types occupy different size in memory and has different alignment requirement.

To elaborate, a pointer variable holds an address which points to some type of data. Without the type associated, there would be no way to dereference the pointer and get the value.

In other words, to access the data pointed to by a pointer, the associated data type must be known.

The size of the pointer itself, has little connection to the actual data it points to.

There exists a pointer , void * which is considered a generic pointer, but then, you can't dereference it, as the result of dereference would attempt to produce an incomplete type. You need to cast it to a complete type to be able to dereference or apply pointer arithmetic on a void pointer.

The reason behind considering void * a generic pointer is as below, quoting from the C11 standard, chapter §6.3.2.3

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

So, a void * can be used as a generic container which can hold any pointer type, but to make some operation on the pointer (which includes the knowledge of the data type), you need to cast it to a complete type first.

Why do variable pointers contain the address of the same data type?

Because when you increase a pointer like

ptr++;

it will point to an address that is one multiplied with the size of the data type. If you do

ptr+=2;

and the data type is occupying 4 bytes which would be the case if the pointer was declared as

float *ptr;

the pointer will increase with 8 byte positions.

Why does a function pointer declaration need to know the types of the parameters and return value?

There are several reasons. Let’s consider the return value first.

Suppose a program contains the code y = f(x);. After calling f, the compiler need to get the value it returned and assign it to y. Where does it get that value? In some systems, integer return values are passed in general registers and floating-point return values are passed in floating-point registers. So the compiler cannot know where the return value is unless it knows the type. Wide integers, such as long long int, might be passed in multiple registers. Small structures might be returned in registers, while large structures might be returned in memory, using a pointer to space the calling function provides and that it is required to pass to the called function as a hidden argument.

Similarly, the compiler needs to know where to put the arguments when it is calling a function. The first several integer arguments might be passed in general registers, while the first few floating-point arguments might be passed in floating-point registers. Additional arguments of either type might be pushed onto the stack.

Additionally, sometimes programmers might pass an integer expression where the function actually expects a floating-point argument, such as in pow(x, 4). When the compiler knows the argument must be a floating-point argument, it can convert it to the expected type.

Another benefit is that, when the compiler knows the types, it can report an error if the argument does not match the expected type and cannot be implicitly converted to the expected type, and it can similarly report an error if the return type does not match.

Datatype declaration significance in pointer to pointer (C/C++)

Why should we use void** when you want a pointer to a char *? Why should we not use char **?

With char **, you have type safety. If the pointer is correctly initialized and not null, you know that by dereferencing it once you get a valid char * - and by dereferencing that pointer, in turn, you get a char.

Why should you ignore this advantage in type safety, and instead play pointer Russian roulette with void**?



Related Topics



Leave a reply



Submit