【发布时间】:2020-06-11 00:54:29
【问题描述】:
我正在编写一个函数,它采用像“HELLO”这样的纯文本和像“YTNSHKVEFXRBAUQZCLWDMIPGJO”这样的键(26 个字母,每个对应于字母表中的一个字母,所以 Y = A、T = B 等)作为输入和返回明文的加密版本。
这是函数:
string encrypt(string plaintext, string key)
{
string e_word; // encrypted text (ciphertext) that will be returned by the function
int index;
for (int i = 0; i < strlen(plaintext); i++)
{
index = get_index(plaintext[i]); // index value is updated with every increment of the loop/ pass of the loop
e_word = strcat(e_word, key[index]);
}
return e_word;
}
错误状态“不兼容的整数到指针转换将 'char' 传递给 'const char *' 类型的参数;使用 & 获取地址”并出现在以下行:
e_word = strcat(e_word, key[index]);
错误提示在“key[index]”之前添加了一个“&”。 我是 C 的新手,一直在研究和使用指针,但我很难理解为什么在这种情况下需要指针。然而,我使用了一个调试器,发现 index 变量确实保存了一个 int(就像我想要的那样),但是当它在“key[index]”中使用时,“key[index]”返回一个内存位置(我相信)。如果我没有声明任何指针来保存变量的内存位置,为什么会发生这种情况?
【问题讨论】:
-
您不会在 C 中“实现”指针,而是使用它们。只有在编写编译器时才能实现它们。
-
这实际上是 C 吗?或者是其他东西? C 没有
string类型。 -
我在想问题出在哪里,
typedef char* string; -
@JWCS 喜欢 cs50?
-
我没学过那个版本,我只熟悉c89+