Question

General syntax of pointer declaration: data-type *pointer_name;

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The data type of pointer must be same as the variable, which the pointer is pointing.

Why is it important that a pointer variable should contain the address of a variable of the same data type?

As a pointer has nothing to do with the value of another variable, why can't an integer pointer have the address of float data type variable?

Correct form:

int a = 10 ;

int *ptr = &a ;

ERROR, type mismatch

float a;

int *ptr; ptr = &a;

Was it helpful?

Solution

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.

OTHER TIPS

Before answering that, let me ask you, if it were valid, what would you do with such a pointer?

Suppose you have (inside a function)

float a;
int *ptr; ptr = &a;

*p = 1;
++*p;
return *p;

In such a case, there is no reason at all not to just use an int variable if it's an int you want.

Suppose you have (inside a function)

float a;
int *ptr; ptr = &a;

a = 3.14;
++*p;
return a;

In this sort of object aliasing, there can be a use, but allowing constructions like this is a pain for compilers. Compilers are free to assume, and some do assume, that the modification of *p has no effect on the value of a, so still return 3.14 unmodified;

If the latter case is something you're looking for, you can use union, which both makes your code clearer to other readers, and makes your code clearer to the compiler:

union {
  float f;
  int i;
} u;
u.f = 3.14;
++u.i;
return u.f;

So, to answer: it's to prevent you from shooting yourself in the foot. It's not allowed because there is no way such a pointer is useful. The one case where it could be useful is actually a case where it doesn't work, and there is another language construct that does handle it, and handles it correctly.

There is also the simple matter of dereferencing the pointer.

something = *pointer;

How much data should be read when the pointer is dereferenced? The compiler must know the data type in order to perform operations on the data like fetch, add, etc.

It's a safety feature. A pointer of one type pointing to a variable of another is a recipe for disaster; it has almost no legitimate purpose and almost anything you do with it is dangerous, especially if the types are of different sizes.

You can override this feature by means of casting. If you do so, you can no longer claim that you didn't know you were doing something dangerous.

It is possible for pointers to point to any data type: that's exactly what void pointers are for. You can use a void * to point to any data type1, and then you can get the original pointer back.

float f = 1.0f;
int i = 12;

void *p = &f;
p = &i;

Of course, you can't dereference a void pointer without first casting it back to the correct pointer type. It is up to you to ensure that the pointer type is correct.

// In C, this is valid: implicit conversions to void * and back.
float f = 1.0f;
void *p = &f;
float *fp = p;
printf("*fp = %f\n", *fp);

// In C++, you have to use a cast:
float *fp = static_cast<float *>(p);

Void pointers have limitations: you cannot dereference them, and you cannot do any pointer arithmetic.

1: Function pointers should not be cast to void *.

Lets understand it with an example.

int main()
{
    char i = 8;
    int *ptr = &i;
    printf("Value of i = %d", *ptr);
    return 0;
}

Ans: It will print some garbage because it dereferenced 4 byte in memory. so if you do char *ptr = &i;, it will dereference 1 byte and so on.. It will give correct answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top