#C Struggling with type cast and type cast pointers

1 messages · Page 1 of 1 (latest)

low skiff
#

I'm struggling to understand typecast and type cast with pointers ```c++
int main() {
int f = 5;
char a = (char)f;
// why use (Char) if it implicitly casts anyway
std::cout << f;
// i dont understand what it means when you are using pointers to cast example below not declared above

int *pointer = (char *)(value + another value)
// another example i see
int *pointer = (int *)(DWORD *)(somevalue + another)
// another
int *pointer = (char *)(somevalue)

return 0;

}``` i have spend weeks and weeks struggling really hard to understand pointer casts please for the love of god

merry kayakBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

frozen crescent
#

@low skiff

    int f = 5;
    char a = (char)f; 

here the variable char a will be equal to the character with ascii code 5

low skiff
#

whats the need for the (char) though when you can implicitly cast it

frozen crescent
#
int *pointer = (char *)(value + another value) 

here you are taking sime int value (value + another value) , then you are accessing the address in memory there and pretending that at that address a char lives, and later you use the same address and pretend that there an int variable lives

#

this causes crashes because you can't just access some random address and hope that you can access it

frozen crescent
low skiff
#

i know its wrong im just trying to understand the line

frozen crescent
low skiff
#

first the char a = (char)f; why do i need (char) if its implicitly converting it anyway

frozen crescent
#

here you are explicit, so you don't get compiler warnings

#

int takes 4 bytes, but char takes 1 byte

low skiff
#

hmmm so if you have more than 4 bytes what would happen

frozen crescent
low skiff
#

brb sorry 2 secs

frozen crescent
low skiff
frozen crescent
#

the warning dissapears

#

because I explicitly convert from int to char

low skiff
#

why though

frozen crescent
# low skiff why though

because if the explicit cast is not used, maybe the programmer is using the wrong types in his program and anything above 255 will be converted back to something that fits in a char

#

this is why the compiler raises a warning

low skiff
#

makes sense can you please explain pointer typecasts

frozen crescent
low skiff
#
int* value = (int*)(something + something else);
frozen crescent
low skiff
#

ive seen it in code

frozen crescent
low skiff
#

i can't

frozen crescent
low skiff
#

its from reversing because im learning cyber sec not allowed to talk about it here

#

But i litterally have no where else to ask

frozen crescent