【发布时间】:2019-08-14 09:26:44
【问题描述】:
Noob,正在研究加密问题,我想遍历 C 中数组中的组合,即 aaa、aab、aac、aba 等,然后将每个组合传递给一个函数(以检查该组合是否是正确的代码)。
我可以打印我想要控制台的内容没有问题,即 aa、ab、ba、bb,但无法将这些值放入我的临时变量中。
#include <stdio.h>
int main(void) {
char *word = "ab";
char * temp[3];
//temp[2] = '\0';
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
temp[0] = &word[i];
temp[1] = &word[j];
printf("%s\n", *temp);
// printf("%c", word[i]);
// printf("%c\n", word[j]);
// pass_temp_to_function(temp);
}
}
return 0;
}
当我应该得到 aa、ab、ba、bb(使用我上面的代码)时,我得到 ab、ab、b、b,但不知道为什么或如何更正它或以其他方式找到答案,因此是我的 Noobish 问题。
【问题讨论】:
-
我想你想要一个
char temp[3];。 -
别忘了初始化
temp,因为C语言中的字符串需要以null结尾。
标签: c for-loop variable-assignment