Do pointer allocate another pointer address ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

16th Dec 2016, 5:47 AM
Kapil Pandey
Kapil Pandey - avatar
4 Answers
+ 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
16th Dec 2016, 6:28 AM
Matthew Shephard
Matthew Shephard - avatar
+ 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').
16th Dec 2016, 7:06 AM
Matthew Shephard
Matthew Shephard - avatar
+ 1
how the data conversion is done,
16th Dec 2016, 6:38 AM
Kapil Pandey
Kapil Pandey - avatar
0
only in double pointers
22nd Dec 2016, 12:01 AM
Fsociety Anonymous
Fsociety Anonymous - avatar