在C中字符串数组不能正确更新

kkih6yb8  于 2024-01-06  发布在  其他
关注(0)|答案(1)|浏览(258)

我有一个for循环,它扫描一个名为text的用户输入字符串,从它的ASCII值中减去一个数字(int键),并将每个字符替换为一个新字符串(textEnc)。
每次for循环时,当前字符和之前的所有字符都会被替换。因此,如果初始字符串是“TEST”,而不应用键减法,输出将是“TTTT”。它将第一个字符替换为第二个字符,然后将第二个字符替换为第一个字符。
为什么会出现这种情况?如何解决?

  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main()
  5. {
  6. // Interface for inputting and storing string and key
  7. string text = get_string("String to encrypt: ");
  8. int length = strlen(text);
  9. int key = 0; // User inputs a key between 1 and 15.
  10. do
  11. {
  12. key = get_int("Enter key [1-15]: ");
  13. } while (key < 1 || key > 15);
  14. string textEnc[length]; // empty array initialised to store the encrypted string. It is set to the same length as the inputted string.
  15. for (int i = 0, j = length; i < j; i++)
  16. {
  17. char asc = text[i] - key;
  18. textEnc[i] = &asc;
  19. }
  20. }

字符串
我试着把一个字符串的内容一个字符一个字符地移到另一个字符串,但是每个“循环”都用当前字符替换它之前的所有字符。
如何将textEnc改为char而不是string数组?

s6fujrry

s6fujrry1#

我试用了您的代码,当我编译代码时,我收到了一个关于试图在字符/整数中存储指针值的警告。

  1. /home/craig/C_Programs/Console/CS_Encrypt/main.c|28|warning: assignment to char from char *’ makes integer from pointer without a cast [-Wint-conversion]|

字符串
争论的焦点,是这一行代码。

  1. textEnc[i] = &asc;


为了更好地说明这里发生的事情,我添加了一些“printf”语句来说明正在发生的过程。

  1. for (int i = 0, j = length; i < j; i++)
  2. {
  3. char asc = text[i] - key;
  4. printf("Text character is: %c Encrypted character is: %c Address of encrypted character is: %p: \n", text[i], asc, &asc);
  5. textEnc[i] = &asc;
  6. printf("textEnc[i] contains: %c which has an integer value of: %d\n", textEnc[i], textEnc[i]); /* The address of the encrypted character is getting placed into the character array */
  7. }
  8. printf("Following is the resulting string: %s\n", textEnc);


下面是一些示例输出,表示在加密文本数组中存储变量“asc”的内存指针的未定义行为。

  1. craig@Vera:~/C_Programs/Console/CS_Encrypt/bin/Release$ ./CS_Encrypt
  2. String to encrpyt: Hello
  3. Enter key [1 - 15]: 2
  4. Text character is: H Encrypted character is: F Address of encrypted character is: 0x7ffc249e766b:
  5. textEnc[i] contains: k which has an integer value of: 107
  6. Text character is: e Encrypted character is: c Address of encrypted character is: 0x7ffc249e766b:
  7. textEnc[i] contains: k which has an integer value of: 107
  8. Text character is: l Encrypted character is: j Address of encrypted character is: 0x7ffc249e766b:
  9. textEnc[i] contains: k which has an integer value of: 107
  10. Text character is: l Encrypted character is: j Address of encrypted character is: 0x7ffc249e766b:
  11. textEnc[i] contains: k which has an integer value of: 107
  12. Text character is: o Encrypted character is: m Address of encrypted character is: 0x7ffc249e766b:
  13. textEnc[i] contains: k which has an integer value of: 107
  14. Following is the resulting string: kkkkk
  15. craig@Vera:~/C_Programs/Console/CS_Encrypt/bin/Release$ ./CS_Encrypt
  16. String to encrpyt: Hello
  17. Enter key [1 - 15]: 2
  18. Text character is: H Encrypted character is: F Address of encrypted character is: 0x7ffdbc272aab:
  19. textEnc[i] contains: which has an integer value of: -85
  20. Text character is: e Encrypted character is: c Address of encrypted character is: 0x7ffdbc272aab:
  21. textEnc[i] contains: which has an integer value of: -85
  22. Text character is: l Encrypted character is: j Address of encrypted character is: 0x7ffdbc272aab:
  23. textEnc[i] contains: which has an integer value of: -85
  24. Text character is: l Encrypted character is: j Address of encrypted character is: 0x7ffdbc272aab:
  25. textEnc[i] contains: which has an integer value of: -85
  26. Text character is: o Encrypted character is: m Address of encrypted character is: 0x7ffdbc272aab:
  27. textEnc[i] contains: which has an integer value of: -85
  28. Following is the resulting string: �����


加密文本数组中的每个元素都是一个字符,您只需要存储在“asc”字符变量中派生的值。

  1. for (int i = 0, j = length; i < j; i++)
  2. {
  3. char asc = text[i] - key;
  4. printf("Text character is: %c Encrypted character is: %c Address of encrypted character is: %p: \n", text[i], asc, &asc);
  5. textEnc[i] = asc; /* Refactored */
  6. printf("textEnc[i] contains: %c which has an integer value of: %d\n", textEnc[i], textEnc[i]); /* The address of the encrypted character is getting placed into the character array */
  7. }
  8. printf("Following is the resulting string: %s\n", textEnc);


重新测试此代码将生成最有可能是所要查找的输出类型的代码。

  1. craig@Vera:~/C_Programs/Console/CS_Encrypt/bin/Release$ ./CS_Encrypt
  2. String to encrpyt: Hello
  3. Enter key [1 - 15]: 2
  4. Text character is: H Encrypted character is: F Address of encrypted character is: 0x7ffe24ba3c4b:
  5. textEnc[i] contains: F which has an integer value of: 70
  6. Text character is: e Encrypted character is: c Address of encrypted character is: 0x7ffe24ba3c4b:
  7. textEnc[i] contains: c which has an integer value of: 99
  8. Text character is: l Encrypted character is: j Address of encrypted character is: 0x7ffe24ba3c4b:
  9. textEnc[i] contains: j which has an integer value of: 106
  10. Text character is: l Encrypted character is: j Address of encrypted character is: 0x7ffe24ba3c4b:
  11. textEnc[i] contains: j which has an integer value of: 106
  12. Text character is: o Encrypted character is: m Address of encrypted character is: 0x7ffe24ba3c4b:
  13. textEnc[i] contains: m which has an integer value of: 109
  14. Following is the resulting string: Fcjjm


从这一点中得到的启示是,要知道什么时候要引用值,什么时候要引用变量的指针。你可能想深入研究一些关于字符数组,字符和指针的用法的“C”教程。

展开查看全部

相关问题