P
Paul
It might happen to be the first (or second or tenth) element of an
array,
but that doesn't influence the pointer type. It's still a pointer to
an
int.
You are clearly confused between:
I'm not at all confused.
a) pointer to int
b) pointer to int-type
Which is the same thing.
There you go , there is the source of your confusion.
It's not the same thing, if you have a pointer pointing to memory
location 0F0000, the entity at this location is what the pointer points
to.
Sorry, in C++ pointers don't point to types. Pointers point to objects.
So, a pointer to int-type is another language.
No, pointers point to an entity, that entity is not necessarrilly an
object.
A pointer-to int-type means exactly the same thing as pointer-to entity
of int-type.
You mean thats what you've been thinking about?The fact that the pointer contains the address of an object, does not
mean that it is a pointer to that object.Consider
union AllTypes {
int Integer;
int IntArray[5];
float Floating;
float FloatArray[18];
etc. etc.
};
AllTypes x;
int * IntPointer = &x.Integer;
Now IntPointer is a pointer to an int object.
IntPointer is not a pointer to a float object.
IntPointer is not a pointer to an float array.
etc. etc.
IntPointer is not a pointer to an int array.
At the end of the day who cares what it's NOT a pointer to, what are you
trying to demonstrate?
--Who cares? I thought you would care.
--If not, finally, you don't care that IntPointer[2] can be used to access
an
--array element,
--but that at the same time IntArray is not a pointer to an array.
I don't know what you are trying to state , sorry.
--OK, let me see if I can say it even more clearly.
--IntPointer is a pointer to an object of type int.
--IntPointer now points to the int x.Integer.
--Because of the union, IntPointer[2] accesses an element of x.IntArray.
But does it? Or this this UB?
--I said that IntPointer is not a pointer to the array x.IntArray.
int* p = x.IntArray is a pointer to the array though.
--You said: Who cares?
--But earlier you said that if a pointer points to a memory location
--that contains an array, it must be a pointer to an array.
Yes this is true. How can it not be a pointer to an array if it points to an
array?
--So, I got the impression that you would care about this.
--Because of the union
--IntPointer also points to a memory location of a float
--and to a memory location of a float array, etc. etc.
If you use the union as a float the memory location does not contain an
array.
--According to your reasoning,
--IntPointer would be also a pointer to a float,
--and to a float array, etc. etc.
No, when the union chnages to represent a new data-type the pointer to the
old data-type is no longer valid IMO.
-After the assignment
- x.Floating=3.14;
-the memory location IntPointer points to, contains a float object.
-Still, IntPointer is not a pointer to a float
-and cannot be used like that without a cast.
As I said above the memory pointed-to would then no longer contain an int or
an array of int. As you say to use the value of this pointer would require a
cast.
--So, the fact that the value of a pointer is an address
--of a memory location that happens to contain an object of some type,
--does not mean that the pointer is a pointer to such an object.
OFC it does.
void* p = &x; // points to x and all its members. The type of pointer
affects how the data will be interpreted when the pointer is dereferenced.
The type of pointer does not define what it points-to.
Your example does not represent an array because arrays and their elements
are of the same type..
With arrays:
int arr[5]; //array of type int.
int x; // variable of type int.
int* p; // can point to both 'an array of int' and 'a single int'.
--Saying that an int* is a pointer to a float is not the intention of the
C++
--language,
--even if the address contained in it happens to be a location of a float.
Well I'm not saying that at all , it's only you that has suggested that.
--So, may I conclude that an int* pointer that happens to contain the
address
--of an int array is not a pointer to an array according to the terminology
of
--C++?
--Or do you still say: Who cares?
An int* that contains the address of an array, points to the array.
Whatever that address happens to be.
If it points-to the array, it points-to the array. How you manage to see
this as not pointing-to the array is very confusing and, though you might
choose to believe otherwise, definately not the correct terminology.