size of char is 1 byte but that of int is 4
If i use int then output for a,b will be a+4*b
in the program the value of ptr is a after the assignment
address of ptr[c] will be ptr +c*sizeof(type)
if the type is char then it equals a+c*1=a+c
value of ptr is stored as a hexadecimal number
so the use (int)ptr in last line
That is why it outputs the sum
size of char is 1 byte but that of int is 4
If i usize of char is 1 byte but that of int is 4
If i use int then output for a,b will be a+4*b
in the program the value of ptr is a after the assignment
address of ptr[c] will be ptr +c*sizeof(type)
if the type is char then it equals a+c*1=a+c
value of ptr is stored as a hexadecimal number
so the use (int)ptr in last line
That is why it outputs the sum
se int then output for a,b will be a+4*b
in the program the value of ptr is a after the assignment
address of ptr[c] will be ptr +c*sizeof(type)
if the type is char then it equals a+c*1=a+c
value of ptr is stored as a hexadecimal number
so the use (int)ptr in last line
That is why it outputs the sum
int main() {
> int a,b;
>> cin>>a>>b;
>>
>> char *ptr = (char *) a;
>> char *ptr2 = &ptr[b];
>> cout<< (int) ptr2 <<endl;
>> return 0;
>> }
>>
>>
>>
>>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---