0

Unable to understand casting through pointers. Why did the output give random value instead of 3.4 (float) or 3 (in case of int)

Also give me some link to practice this and pointers(arrays) please https://code.sololearn.com/ckBnO21KiC6g/?ref=app

9th Sep 2018, 12:07 PM
Maleeha Khalid
Maleeha Khalid  - avatar
9 Antworten
+ 2
thanks a lot Sergey Ushakov and Robert Atkins
9th Sep 2018, 2:52 PM
Maleeha Khalid
Maleeha Khalid  - avatar
+ 1
The value is not random. It is your flat value, readded as integer. Int and float values has completely different representation.
9th Sep 2018, 2:33 PM
Sergey Ushakov
Sergey Ushakov - avatar
+ 1
Iterator casting doesn't overwrite the variable at the location it coverts the data type from 1 to another if you want to just assign a position via pointer you can do this https://code.sololearn.com/cg6yFkEDdUBO/?ref=app ill try to figure out your other questions! :)
9th Sep 2018, 2:35 PM
StealthyTaco
StealthyTaco - avatar
0
You are not casting value, but writing and reading them in the same place at memory. Change line 8 by adding a cast: *((int*)p+3)=3;
9th Sep 2018, 2:02 PM
Sergey Ushakov
Sergey Ushakov - avatar
0
man i just tried so many things! i think the issue is the casting because if you make the array a float array and have a float pointer it works fine, but for some reason when you cast from an int to a float* sh*t gets real. pointers can be very dangerous to a program if you arent careful! sorry I couldnt of been more help! the only thing i can think of which would be a pain would be to create a for loop and cast each individual value of the int array into the float array. idk if you know or not but arrays in c++ are basically pointers to a set of data, so I think trying to conver an int pointer to a float pointer is the main issue.
9th Sep 2018, 2:04 PM
StealthyTaco
StealthyTaco - avatar
0
Sergey Ushakov as far as i know, casting means to overwrite the already asigned value..like i tried to cast int at location 3(whatever, i know you got my point) to float..these are the typical exam questions I'm unable to comprehend although i know the concepts of how these work :(
9th Sep 2018, 2:13 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Robert Atkins i got your point but i want to know why the value does not change to 3 when i try to get values through loop and another question is what is the point of type casting? why do we need it?
9th Sep 2018, 2:15 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Sergey Ushakov 1 more question is that if we used pointer to change the value and accessed it through pointer ,it is same (3.4) but through array index ,it value is random (not 3/3.4).WHYYY :(
9th Sep 2018, 2:19 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
There are no "assigned value" when you are using low level language. Just memory, that you are writing and reading. You are unable to change type of element of array and store float value to it. It is because the array declares how to access the memory.
9th Sep 2018, 2:24 PM
Sergey Ushakov
Sergey Ushakov - avatar