【发布时间】:2021-02-27 07:43:16
【问题描述】:
我在谷歌上搜索了一个使用 2 个数组并打印输出而不重复字符的练习,然后我发现了这个更容易阅读和修改的练习,但问题是我不明白 int temp 的含义[256 + 128] = {0}; 和 temp[+str[i]] = 1;
这是完整的代码
#include <unistd.h>
void remove_dup(char *str, char *str2)
{
int temp[256 + 128] = {0};
int i;
i = 0;
while (str[i])
{
if (temp[(int)str[i]] == 0)
{
temp[+str[i]] = 1;
write(1, &str[i], 1);
}
i++;
}
i = 0;
while (str2[i])
{
if (temp[+str2[i]] == 0)
{
temp[+str2[i]] = 1;
write(1, &str2[i], 1);
}
i++;
}
}
int main(int argc, char **argv)
{
if(argc == 3)
remove_dup(argv[1], argv[2]);
write(1, "\n", 1);
return(0);
}
【问题讨论】:
-
256 + 128表明代码设计用于签名的char或unsigned char,而+str[i]和+str2[i]应该是128+str[i]和128+str2[i]。