+ 1
Do pointer allocate another pointer address ?
what if a pointer int*X stores address of another pointer char*class = int*X which holds the address of 1st pointer. i mean if 2 pointers have address of each other in a programme then output of value int*X will be.
4 ответов
+ 2
It has been a while since I've worked with C++, but if I recall my theory correctly, a pointer pointing to a pointer would resolve to the initial target's memory address, regardless of its contents.
Using your example, however, a typical assignment of int* X = char* Y would allocate memory for an int, find the memory location of the char* Y and attempt to assign that as its target, resulting in an error as char* Y must point to a char, int* X must point to an int and you have not included any form of data conversion to handle that datatype difference. I do not recall off the top of my head if the error would throw at compile time (invalid data reference/conversion) or in processing (first time trying to evaluate the value of X when I determines it has found something that is indeed not an Integer
+ 2
Actually, I had forgotten a part in that...
A pointer declared to point to a pointer must have two asterisks to indicate it is pointing to a pointer. We must also use the MEMORY ADDRESS (&) of the pointers, so it would be something like:
char *pValue;
int **ppValue;
char value = '7';
pValue = &value;
ppValue = &pValue;
Now, to use the data, int intValue = **ppValue; would actually set intValue as 55!
So, to use the integer version of the char, we could take advantage of the fact that the numbers in the ASCII table are sequential (starting at 48) with:
int intValue = **ppValue - '0' //the character '0' is char 48 in the ASCII table, so this means 7 is char 55... This essentially means int intValue = 55-48; returning intValue = 7. You could also explicitly approach it numerically as int intValue = **ppValue - 48; same result either way whether you use int 48 or char 48 ('0').
+ 1
how the data conversion is done,
0
only in double pointers