需要写入字符串常量,如何解决这个问题?

发布于 2024-08-19 02:45:37 字数 583 浏览 5 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合久必婚 2024-08-26 02:45:37

您正在声明一个指针数组,而不是一个字符数组(通常用作字符串)。

您需要这样声明:

char sPPhase[51];

另外, sscanf 可能会给您带来麻烦:最好使用 fgets 读取有界缓冲区中的字符串:

int main() {
    char sPPhrase[51];
    printf("Enter string (max. 50 chars):\n");
    fflush(stdout);
    fgets(sPPhrase, 50, stdin);  // leave one byte for '\0'

    // More code
}

我不知道“reverse”在做什么,但您可能应该将其定义为:

char* reverse(char* sPPhrase);

如果它就地执行操作,您甚至不需要返回值。如果这样做,请不要忘记在完成后释放它。

You are declaring an array of pointers, not a array of chars (commonly used as a string).

You need to declare like this:

char sPPhase[51];

Also, sscanf can get you in trouble: it's better to use fgets to read a string in a bounded buffer:

int main() {
    char sPPhrase[51];
    printf("Enter string (max. 50 chars):\n");
    fflush(stdout);
    fgets(sPPhrase, 50, stdin);  // leave one byte for '\0'

    // More code
}

I don't know what 'reverse' is doing, but you should probably define it as:

char* reverse(char* sPPhrase);

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.

居里长安 2024-08-26 02:45:37

您对 sPPhase 的描述:

char *sPPhrase[51];

实际上是一个包含 51 个指针的数组。
您真正想要的只是一个字符数组:

char sPPhrase[51];

当您这样做时,您应该更改 scanf

scanf("%s",sPPhrase)

另请注意,您的 scanf 可能会读取比您预期的更多的内容。

Your decaration of sPPhase:

char *sPPhrase[51];

Is actually an array of 51 pointers.
What you actually want is just an array of characters:

char sPPhrase[51];

When you do that, you should change the scanf

scanf("%s",sPPhrase)

Also note that your scanf might read more than you expect.

围归者 2024-08-26 02:45:37

要理解这一点,您需要回到数组在内存中的实现方式。 char* sPPhrase[51]; 是指向指针的指针的声明,您可以将其视为类似于二维数组。如果声明这一点并调用 scanf 来读取它,则将整个数组的值设置为等于一个字符。这就像在说:

char chars2D[50][50];
chars2D[0] = 'A';

这样做是将整个数组设置为“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:

char chars2D[50][50];
chars2D[0] = 'A';

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文