What does (char *) x or (void *) z mean?

后端 未结 7 497
别那么骄傲
别那么骄傲 2021-01-22 03:47

I know char *x means a pointer to char, but I\'m confused about what (char*) x means.

相关标签:
7条回答
  • 2021-01-22 04:15

    declaration of a variable called "x" of type char:

    char x;
    

    declaration of a variable called "x" which is a pointer to a char:

    char *x;
    

    cast of something that is not a char to type char:

    int x = 10;
    char y = (char)x;
    
    0 讨论(0)
  • 2021-01-22 04:18

    () is the cast operator.

    (char *) x means "apply the cast operator to operand x".

    The cast operator converts the value of the operand to the type between ().

    0 讨论(0)
  • 2021-01-22 04:23

    Type casting to character pointer.

    0 讨论(0)
  • 2021-01-22 04:23

    Actually, char *x is a declaration. x is pointer to char.

    If you already have a variable, such as x, you can cast it to a different type. In your case, (char*) x is redundant, since x already is a pointer to char. But you can cast it to int * or something else. Although, note, it's not safe, since an int is larger than a char.

    0 讨论(0)
  • 2021-01-22 04:25

    It means casting x to a pointer to char (or to a general pointer).

    0 讨论(0)
  • 2021-01-22 04:27

    It's a cast. You are instructing the compiler to treat x as if it were a char *, regardless of its real type. Casts should only be used if you really know what you are doing.

    For some built-in types, the compiler may perform a meaningful conversion, e.g. converting a double to an int by rounding, but for other types you may not get what you expect.

    0 讨论(0)
提交回复
热议问题