Join Regular Classroom : Visit ClassroomTech

Why it is not possible to store an integer address in a float pointer in C?

We need to specify the type of the data whose address a pointer will hold, though all pointers are similar as they hold the addresses. The amount of space required by a pointer to store the address depends on the machine whether it is 32 bit or 64 bit.

Suppose we have a pointer named ptr it contains a random address but it does not specify that whether it is an address of a char or int or double or float. A pointer variable on a 64-bit architecture occupies 8 bytes, no matter what type of pointer it is.

C compiler needs to know the type of the variable more than its size, because long and float both need 4 bytes, but we have to specify the compiler which one we want because their operations are different. An integer pointer can store the address of a float variable, but it shouldn’t.

You specified that your pointer variable points to an entity of type int when you constructed the pointer. If you had intended to point to a variable of type float, then your pointer should have been declared as a float pointer. The compiler will simply implement all pointer types as a word-length buffer, holding an address. The type information of a pointer is purely a compile-time hint about what the compiler should and should-not allow to be done to the pointer and the entity it references.

Example

/* C program to understand why it is not possible to store an integer address in a float pointer */
/* www.codewindow.in */

#include <stdio.h>

int main() {
    float *ptr;     // Pointer of float type, this can hold the address of a float type variable.
    int var = 20;    // value is assigned to a int variable
	ptr= &var;	// Assigning the address of variable var to the float pointer *ptr.

   	printf("Value of variable var is: %d", var);
   	printf("\nValue the pointer gives  is: %d", *ptr);
   	
   	return 0;
}

Output:
[Warning] assignment from incompatible pointer type
[Warning] format “%d” expects argument of type int, but argument 2 has type float
Value of variable var is: 20
Value the pointer gives is: 2147483619

Explanation: Address of the variable will be the output because of assigning the float type pointer to hold the int type address

Don’t know what is pointer to pointer in C? Read: Pointer to a pointer (Double Pointer) in C 

If we assign a integer type variable with an int type pointer then no warning will be shown and the *ptr will also return the value of the variable not it’s address.

Example

#include <stdio.h>

int main() {
   	int *ptr;	// Pointer of integer type, this can hold the address of a integer type variable.
	int var = 20;
	
	ptr= &var;	/* Assigning the address of variable var to the pointer ptr. 
			       The ptr can hold the address of var because var 
			       is an integer type variable. */

   	printf("Value of variable var is: %d", var);
   	printf("\nValue of variable var is: %d", *ptr);
   	
    return 0;
}

Output:
Value of variable var is: 20
Value of variable var is: 20

Explanation:
*ptr => value at address (ptr) => value at address of the variable var => 20


You Missed

Also Checkout