c - 字符串变量在 C 中未正确返回

标签 c string function return return-value

我一直在尝试创建一个函数 is_isogram,返回一个按字母顺序排列的字符串值。当我通过 main 中的函数传递值时,要返回的变量被设置为正确的值。

但是,当我从函数返回变量时,它不会在 main 中返回任何内容。与此同时,当我初始化变量以在 main 中保存返回值时,我收到了 unable to read memory 错误。

#include <stdbool.h>
#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <Windows.h>
#include "isogram.h"

char  is_isogram(char phrase[])
{
    int i;
    for (i = 0; phrase[i]; i++) {
        tolower(phrase[i]);
    }

    if (phrase == NULL) {
        return false;
    }
    char temp;
    int count1;
    int count2;
    char *alphabet = (char *)malloc(sizeof(char) * (strlen(phrase) + 1));
    int n = strlen(phrase);
    for (count1 = 0; count1 < n - 1; count1++) {
        for (count2 = count1 + 1; count2 < n; count2++) {
            if (phrase[count1] > phrase[count2]) {
                temp = phrase[count1];
                phrase[count1] = phrase[count2];
                phrase[count2] = temp;
                alphabet = phrase;
                break;
            }
        }
    }
    return alphabet;
}

此函数不返回变量 alphabet。我一直在尝试使用 malloc 和其他技术将这个局部变量作为值返回。我想将每个字符串用作带有“[]”的数组,但是我会收到关于需要初始化数组的错误。返回的值目前只是一个相当大的垃圾值,但是,当我逐步调试过程时,变量 alphabet 是“abb”。

int main() {
    char *bet = is_isogram("bab\0");
    if (bet == 'abb\0') {
        return 0;
    } else {
        return -1;
    }
}

变量 bet 甚至在访问函数之前就抛出错误。它告诉我它无法读取内存,并且对于程序的其余部分,bet 的值接近于 NULL

总而言之,当从本地函数返回值“abb”时,程序没有返回值,也没有将变量保存在内存中。它向变量中抛出垃圾,但变量被杀死,并且无法在 main 中充分初始化新变量。

最佳答案

您的代码中存在多个问题:

  • 函数 is_isogram() 被定义为返回一个 char,它应该返回一个字符串,因此类型为 char *
  • is_isogram() 尝试修改其参数指向的字符串。由于它是使用 main 中的字符串文字调用的,因此具有未定义的行为,可能会导致程序崩溃。
  • 您为 alphabet 分配内存,但在排序循环中将此指针重置为 phrase
  • 比较 bet == 'abb\0' 并不像您认为的那样。 bet 应该是 char * 并且您应该使用 strcmp() 来比较字符串值。

修改后的版本:

#include <ctype.h>
#include <string.h>
#include <stdio.h>

char *is_isogram(const char *phrase) {
    char *alphabet;
    int count1, count2, n;

    if (phrase == NULL)
        return NULL;
    alphabet = strdup(phrase);
    n = strlen(alphabet);
    for (count1 = 0; count1 < n; count1++)
        alphabet[count1] = tolower((unsigned char)alphabet[count1]);
    for (count1 = 0; count1 < n - 1; count1++) {
        for (count2 = count1 + 1; count2 < n; count2++) {
            if (alphabet[count1] > alphabet[count2]) {
                char temp = alphabet[count1];
                alphabet[count1] = alphabet[count2];
                alphabet[count2] = temp;
            }
        }
    }
    return alphabet;
}

int main(void) {
    char *bet = is_isogram("bab");
    if (strcmp(bet, "abb") == 0) {
        return 0;
    } else {
        return -1;
    }
}

关于c - 字符串变量在 C 中未正确返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51584871/

相关文章:

检查 docker alpine musl 版本

c++ - 如何在 rstudio 上为使用子目录内 src 文件夹中的 C/C++ 文件的包构建和加载共享库?

c - GTK 字符数与字节索引

c - 将二进制文件作为字符串存储在我的代码中

C 结构体和链表之争

c++ - StringPiece/StringRef 习语不受欢迎有什么原因吗?

python - 将 Python 列表拆分为更小的子字符串

c++ - 函数 try block 是否允许我们解决异常?

将变量传递到函数中的 ggplot 中的正确方法,不要将其解释为 R 中的字符串

php - 输出函数 mysql php