返回类型和数组大小分配问题

发布于 2025-01-31 04:28:05 字数 1401 浏览 1 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

み格子的夏天 2025-02-07 04:28:05

如注释中所述,您正在将其写入零指针,因为您将 ciphertext 设置为null。您要做的是为 ciphertext 分配内存。这可以使用 char *ciphertext = strdup(text); 设置 ciphertext ,并带有新分配的指针,可用于null终止的char指数文本。使用此解决方案或下面的解决方案,您不需要 //存储非按字母的值,因为它已经存在,因为该值已经存在。

使用 strdup()是一种方法,但是如果您可以修改 text 本身,则有一个更好的选择。您可以简单地使用:

void encrypt(char *text, int k) {
    while (*text) {
        // Identify if the value is an alphabet
        if (isalpha(*text) != 0) {
            if (isupper(*text) != 0) {
                // Apply formula to uppercase character
                *text = ((*text - 'A' + k) % 26) + 'A';
            } else {
                // Apply formula to lowercase character
                *text = ((*text - 'a' + k) % 26) + 'a';
            }
        }
        text++;
    }
}

一些解释: while(*text)表示以下内容,直到存储 at text是0。 text ++; 增加指针。因此,在该行之后,文本指向下一个字符。所有的操作只是(int i = 0; text [i]!='\ 0'; i ++)的另一种做法,但不需要新变量。

请注意,以上代码不再返回“加密”的结果。相反,它修改了您传递的 char *的内容。无需内存分配 - 呼叫者为您做到了。

如果您选择使用上述代码,则需要执行类似的操作来打印结果:

...
char *text = get_string("plaintext:  \n");
encrypt(text, key);
printf("ciphertext: %s\n", text);
...

As mentioned in the comments, you are writing to a NULL pointer since you set ciphertext to NULL. What you want to do is allocate memory for ciphertext. This can be done with char *ciphertext = strdup(text); which sets up ciphertext with a newly allocated pointer to a copy of the null-terminated char pointer named text. Using either this solution or the one below, you don't need to // Store non-alphabetical value as it is since that value is already there.

Using strdup() is one method, but there is a better option if you can modify text itself. You can simply use:

void encrypt(char *text, int k) {
    while (*text) {
        // Identify if the value is an alphabet
        if (isalpha(*text) != 0) {
            if (isupper(*text) != 0) {
                // Apply formula to uppercase character
                *text = ((*text - 'A' + k) % 26) + 'A';
            } else {
                // Apply formula to lowercase character
                *text = ((*text - 'a' + k) % 26) + 'a';
            }
        }
        text++;
    }
}

A little explanation: while (*text) means do the following until the value stored at text is 0. text++; increments the pointer. So after that line text is pointing to the next char. All that does is just another way of doing for (int i = 0; text[i] != '\0'; i++) but without needing a new variable.

Note that the above code no longer returns the result of the "encryption". Rather, it modifies the content of the char * you passed it. No memory allocation necessary-- the caller did it for you.

If you opt to use the above code, you'll need to do something like this to print the result:

...
char *text = get_string("plaintext:  \n");
encrypt(text, key);
printf("ciphertext: %s\n", text);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文