well , i know java, but im new to c.

i came across this while learning c :

int my_function(const char *str1){

unsigned char c1;

c1 = (*(unsigned char *) str1);


well i suppose this code is casting from a string to the character value of it but what about the * symbols.

i now know a bit about pointers (i.e. int *ip where ip is a pointer to an integer type) but what's with this line of code :c1 = (*(unsigned char *) str1);

thx in advance :)

9 Years
Discussion Span
Last Post by wonder_laptop

*str is the character pointed to by str , which has type const char . The (unsigned char *) cast tells the compiler to convert str from a pointer to a const plain char to a pointer to an unsigned char (the "conversion" itself is likely trivial, though I won't say "it pretends" or other variants of this phrase that some might use). The leftmost * is used to dereference this pointer; that is, obtain the value of the unsigned char being pointed to.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.