C中的星号有什么作用?

Eme*_*ire 0 c

C中的*是什么意思?我看到它在声明 char 或 FILE 变量时使用 ( char = *test) 这如何改变变量的行为方式?

eml*_*lai 6

它取消引用一个指针:

*ptr = 42; // access the value that ptr points to, and set it to 42
Run Code Online (Sandbox Code Playgroud)

或者它声明一个指针:

int* ptr; // the type of ptr is pointer to int
Run Code Online (Sandbox Code Playgroud)

  • 不,`*p = 42`不会分配给`p`,它分配给`*p`(即`p`指向的东西)。是的,如果“p”没有指向有效的内存,那就是未定义的行为。 (2认同)

Mik*_*CAT 5

这种类型*称为“间接运算符”,*test意思是“从指针test指向的地方获取数据”。

char保留用作关键字,因此char = *test除非char定义为宏,否则不会编译。