为什么超过44个字符时会打印随机符号 [英] Why does it print random symbols when exceeding 44 characters

查看:83
本文介绍了为什么超过44个字符时会打印随机符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一本书《 C编程:现代方法》中学习C。现在,我将进行有关数组的深入研究。练习之一是编写一个过滤器,以不同的方式打印输入消息。

I'm learning C from a book C programming:Modern approach. Right now I'm going trough exercises about arrays. One of the exercises is to write a filter that prints the input message differently.

我到目前为止(请参见下面的代码),一切正常,直到字符数超过44,然后打印随机符号。如果字符数小于44,则一切正常。我绝对不知道为什么要这么做。问题出在哪里,怎么解决?

I've gotten so far (see the code below), everything works fine, until the character count exceeds 44, then it prints random symbols. If the character count is below 44 everything works fine. I have absolutely no clue why it does that. Where is the problem and what might be the solution?

int i = 0, k = 0;
char message[k],ch;

printf("Enter a message: ");
while(toupper(ch = getchar()) != '\n')
{
    message[k] = ch;
    k++;
}
printf("In B1FF-speak: ");
for (i = 0; i <= k - 1; i++)
{
    switch(toupper(message[i]))
    {
        case 'A':
            printf("4");
            break;
        case 'B':
            printf("8");
            break;
        case 'E':
            printf("3");
            break;
        case 'I':
            printf("1");
            break;
        case 'O':
            printf("0");
            break;
        case 'S':
            printf("5");
            break;
        default:
            printf("%c", toupper(message[i]));
            break;
    }
}


推荐答案

int i = 0, k = 0;
char message[k],ch;

您已将消息定义为可变长度数组(VLA)。 (此功能在1990年的C版本中不存在;它是1999年标准添加的,并在2011年标准中使其成为可选功能。)

You've defined message as a variable length array (VLA). (This is a feature that doesn't exist in the 1990 version of C; it was added by the 1999 standard and made optional by the 2011 standard.)

k 的值是 0 消息的长度是 0 。 C不支持零长度数组。定义常量长度为零的数组是非法的(违反约束,需要诊断)。定义长度为零的可变长度数组具有未定义的行为。

Since the value of k is 0, the length of message is 0. C does not support zero-length arrays. Defining an array with a constant length of zero is illegal (a constraint violation, requiring a diagnostic). Defining a variable length array with a length of zero has undefined behavior.

VLA的长度在定义时是固定的。稍后更改 k 的值不会更改数组的长度。

The length of a VLA is fixed when it's defined. Changing the value of k later on does not change the length of the array. Your code seems to assume that it will.

您的程序似乎的工作长度最多为44。这就是未定义行为的本质。可能发生的最坏事件是您的程序似乎在正确地工作。

Your program seems to work for lengths up to 44. That's the nature of undefined behavior. The worst thing that can happen is that your program seems to work "correctly"; that just means that you have a bug that's difficult to detect.

如果要在数组中存储任意多个元素,则可以使用较大的大小来定义它首先需要足够的空间(很难或不可能确定它的大小),或者可以使用 realloc()动态分配数组并扩展它如所须。 ( realloc()实际上并没有扩展数组;它创建了一个更大的新数组,并将旧数组的内容复制到新数组中。如果没有足够的可用内存则失败;请始终检查它返回的值以确定它是成功还是失败。)

If you want to store arbitrarily many elements in an array, you can either define it with a size that's big enough in the first place (it can be difficult or impossible to determine how big it has to be), or you can use realloc() to allocate the array dynamically and expand it as needed. (realloc() doesn't actually expand the array; it creates a new array with a larger size and copies the contents of the old array into the new array. And it can fail if there isn't enough available memory; always check the value it returns to determine whether it succeeded or failed.)

这篇关于为什么超过44个字符时会打印随机符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆