char (*ptr)[10];
scanf("%s",ptr);//inputing a string
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?据我说这应该工作,因为ptr是一个指向字符数组的指针.
指向数组的指针不是数组,你无处可放你的字符.
这就像没有房子的门垫,这并不意味着你有一个接待客人的地方.
要完成上述工作,你应该这样做
char ptr[10]; // This is where you have space, specifically stack space
char (*this_is_a_pointer_to_array)[10]; // This only holds space to keep an address to an array
this_is_a_pointer_to_array = &ptr;
scanf("%s",ptr);
Run Code Online (Sandbox Code Playgroud)
虽然在上面的例子中你并不真正需要指向数组的指针.
指向数组的指针只保留了将地址保存到数组所需的空间,没有空间存储除地址之外的任何内容.如果你可怕地绕过类型转换机制,你可能会使用该空间来存储一些字符而不是地址,但这违背了我身体的每一个道德标准,也可能违反每个类型转换规则.