gpt4 book ai didi

char 双指针混淆行为

转载 作者:行者123 更新时间:2023-11-30 14:43:23 25 4
gpt4 key购买 nike

我正在 https://www.codingame.com/ 做练习练习一些 C 指针。

任务是将输入字符转换为 ASCII 艺术。例如字母A是:

 # 
# #
###
# #
# #

https://www.codingame.com/ide/puzzle/ascii-art

整个 ASCII 字母表作为不同行的单个输入提供(A 到 Z 加?):

 #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ### 
# # # # # # # # # # # # # # # # # ### # # # # # # # # # # # # # # # # # # # # # # # #
### ## # # # ## ## # # ### # # ## # ### # # # # ## # # ## # # # # # # ### # # # ##
# # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # ### # # # #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #

我试图将字母表存储为双指针(alphabet_input)。

char ** alphabet_input;

int main()
{
int length = 4;
int height = 5;

alphabet_input = (char **)malloc(sizeof(char *)*height);

for (int i = 0; i < height; i++) {
char ROW[1025];
fgets(ROW, 1025, stdin);

alphabet_input[i] = ROW;

// print alphabet lines for the first time
printf("%s", alphabet_input[i]);
}

// print alphabet lines for the second time
printf("%s", alphabet_input[0]);
printf("%s", alphabet_input[1]);
printf("%s", alphabet_input[2]);
printf("%s", alphabet_input[3]);
printf("%s", alphabet_input[4]);

return 0;
}

但是,当我尝试打印它时,我反复收到最后一行。这就是我得到的结果:

 #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ### 
# # # # # # # # # # # # # # # # # ### # # # # # # # # # # # # # # # # # # # # # # # #
### ## # # # ## ## # # ### # # ## # ### # # # # ## # # ## # # # # # # ### # # # ##
# # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # ### # # # #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #

我希望将输入打印两次:

 #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ### 
# # # # # # # # # # # # # # # # # ### # # # # # # # # # # # # # # # # # # # # # # # #
### ## # # # ## ## # # ### # # ## # ### # # # # ## # # ## # # # # # # ### # # # ##
# # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # ### # # # #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #
# ## ## ## ### ### ## # # ### ## # # # # # ### # ## # ## ## ### # # # # # # # # # # ### ###
# # # # # # # # # # # # # # # # # ### # # # # # # # # # # # # # # # # # # # # # # # #
### ## # # # ## ## # # ### # # ## # ### # # # # ## # # ## # # # # # # ### # # # ##
# # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # ### # # # #
# # ## ## ## ### # ## # # ### # # # ### # # # # # # # # # ## # ### # # # # # # ### #

我应该如何操作alphabet_input的索引以正确地将每一行打印为字符串?

我真的很困惑,因为在 for 循环内打印似乎工作得很好。然而,在此之后索引似乎崩溃了。

最佳答案

for (int i = 0; i < H; i++) {
char ROW[1025];
fgets(ROW, 1025, stdin);
alphabet_input[i] = ROW;
}

变量ROW位于 for 主体的范围内,因此它在每次 for 迭代时创建和销毁。您点alphabet_input[i]这个变量在迭代结束时立即被销毁,因此最终会得到悬空指针。

而不是指向alphabet[i]对于局部变量,您需要为其分配空间,然后将该行的内容复制到其中。

char ** alphabet_input;
alphabet_input = malloc(height * sizeof *alphabet_input);

for (int i = 0; i < height; i++) {
char row[1025];
fgets(row, 1025, stdin);

size_t len = strlen(row);

alphabet_input[i] = malloc(len + 1);
strcpy(alphabet_input[i], row);
}

您可能需要根据您想要的长度来调整上面的代码(例如 1025 而不是 strlen,或者删除尾随的换行符等)

我想说的几点:

  • 这就是我建议编写malloc的方式:

    pointer_var = <no cast> malloc(<num_elements> * sizeof *pointer_var);
    alphabet_input = malloc(height * sizeof *alphabet_input);
  • sizeof(char)保证为 1按照标准,您可以跳过 sizeof(char)malloc :

    alphabet_input[i] = malloc(len + 1);
  • 避免所有大写变量。它们通常用于 C 中的宏。

关于char 双指针混淆行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53952006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com