需要写入字符串常量,如何解决这个问题?
我有以下代码:
int main() {
char *sPPhrase[51];
/* Input */
printf("Enter string (max. 50 chars):\n");
fflush(stdout); /* Works around an annoying Eclipse bug that fails to display the output from the printf command */
scanf("%s", *sPPhrase); /* Won't work */
/* More code goes here */
}
我认为 scanf()
命令失败,因为 *sPPhrase 不可写,因为 sPPhrase 指向字符串常量。编译器没有任何错误的线索。稍后,我需要将此字符串传递给此函数:
char* reverse(char* sPPhrase[]);
该字符串常量不可写,但我需要将此 char* 传递给这个功能。如何重写我的代码以使其正常工作?
I have the following code:
int main() {
char *sPPhrase[51];
/* Input */
printf("Enter string (max. 50 chars):\n");
fflush(stdout); /* Works around an annoying Eclipse bug that fails to display the output from the printf command */
scanf("%s", *sPPhrase); /* Won't work */
/* More code goes here */
}
The scanf()
command fails, I assume, because *sPPhrase is not writable as sPPhrase points to a string constant. The compiler doesn't have a clue of anything being wrong. A little later on, I need to pass this string to this function:
char* reverse(char* sPPhrase[]);
The string constant is not writable, but I need to pass this char* on to this function. How do I rewrite my code to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在声明一个指针数组,而不是一个字符数组(通常用作字符串)。
您需要这样声明:
另外, sscanf 可能会给您带来麻烦:最好使用 fgets 读取有界缓冲区中的字符串:
我不知道“reverse”在做什么,但您可能应该将其定义为:
如果它就地执行操作,您甚至不需要返回值。如果这样做,请不要忘记在完成后释放它。
You are declaring an array of pointers, not a array of chars (commonly used as a string).
You need to declare like this:
Also, sscanf can get you in trouble: it's better to use fgets to read a string in a bounded buffer:
I don't know what 'reverse' is doing, but you should probably define it as:
If it is doing the operation in place, you don't even need a return value. If you do, don't forget to free it when you are done.
您对 sPPhase 的描述:
实际上是一个包含 51 个指针的数组。
您真正想要的只是一个字符数组:
当您这样做时,您应该更改 scanf
另请注意,您的 scanf 可能会读取比您预期的更多的内容。
Your decaration of sPPhase:
Is actually an array of 51 pointers.
What you actually want is just an array of characters:
When you do that, you should change the scanf
Also note that your scanf might read more than you expect.
要理解这一点,您需要回到数组在内存中的实现方式。
char* sPPhrase[51];
是指向指针的指针的声明,您可以将其视为类似于二维数组。如果声明这一点并调用 scanf 来读取它,则将整个数组的值设置为等于一个字符。这就像在说:这样做是将整个数组设置为“A”,因此该数组的内存地址为“A”。这是内存中的垃圾值。当您调用
scanf("%s", *sPPhrase);
时,您只是尝试将每个数组的顶部设置为等于一个字母,从而使问题更加严重。所以你得到的是垃圾。Here 是一个描述如何使用 scanf 读取字符数组的线程。
To understand this, you need to go back to how arrays are implemented in memory.
char* sPPhrase[51];
is a declaration of a pointer to pointers, which you can think of as similar to a two-dimensional array. If you declare this and call scanf to read into it, you set the value of an entire array equal to one character. This is like saying:What this is doing is setting an entire array equal to 'A', so that the memory address of the array is 'A'. This is a garbage value in memory. When you call
scanf("%s", *sPPhrase);
you are just multiplying the problem by attempting to set the top of each array equal to a letter. So you get garbage.Here is a thread describing how to use scanf to read into an array of characters.