Another Question about pointers

I'm a little confused about an aspect of declaring pointer type variables. Not so much the what, as the why.

The tutorial says This:

"Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going point to. It is not the same thing to point to a char than to point to an int or a float."

The problem I have with this, is that I don't understand WHY it is not the same thing, to point to different types of variables.

I understand that different types occupy different amounts of memory, but as I understand, a pointer holds a number, which is the first memory cell used.by that variable.

Therefore, I can't see why pointers would be anything more than an integer. Do they also hold data on the size of the memory block referred to? if so, why?
They do and don't. They do find out the size of the data type they point to so that they work properly in pointer arithmetic. For instance, take a look at this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int myIntArray[10];
int *myIntPointer = myIntArray; //myIntPointer now points to the first element in myIntArray
*myIntPointer; // first element
*(myIntPointer+3); // third element
myIntPointer - (myIntPointer + 3); // Should be 12, or 3 * sizeof(int)

double myDoubleArray[10];
double *myDoublePointer = myDoubleArray; //myDoublePointer now points to the first element in myDoubleArray
*myDoublePointer; // first element
*(myDoublePointer+3); // third element
myDoublePointer - (myDoublePointer + 3); // Should be 24, or 3 * sizeof(double)

char myCharArray[10];
char *myCharPointer = myCharArray; //myCharPointer now points to the first element in myCharArray
*myCharPointer; // first element
*(myCharPointer+3); // third element
myCharPointer - (myCharPointer + 3); // Should be 3, or 3 * sizeof(char) 
Topic archived. No new replies allowed.