我在谷歌上搜索了一个使用两个数组并在没有字符重复的情况下打印输出的练习,然后我发现这个更容易阅读和修改,但问题是我不理解int temp256 + 128 = {0};和temp[+stri] = 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);
}发布于 2020-11-16 03:48:37
这是:
int temp[256 + 128] = {0};创建大小为int 256+128 = 384的数组,显式地将第一个元素初始化为0,并将其余元素隐式初始化为0。
这是:
temp[+str[i]] = 1;包含一元+运算符的示例,它类似于一元-运算符。该运算符实际上不执行任何操作,因此该表达式与以下内容相同:
temp[str[i]] = 1;它将str[i]用作数组temp中的索引,并将值1分配给该元素。
https://stackoverflow.com/questions/64852429
复制相似问题