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

kkih6yb8  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(183)

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

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main()
{
   // Interface for inputting and storing string and key

   string text = get_string("String to encrypt: ");
   int length = strlen(text);

   int key = 0;   // User inputs a key between 1 and 15.
   do
   {
      key = get_int("Enter key [1-15]: ");
   } while (key < 1 || key > 15);

   string textEnc[length]; // empty array initialised to store the encrypted string. It is set to the same length as the inputted string.

   for (int i = 0, j = length; i < j; i++)
   {
      char asc = text[i] - key;
      textEnc[i] = &asc;
   }

}

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

s6fujrry

s6fujrry1#

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

/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]|

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

textEnc[i] = &asc;


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

for (int i = 0, j = length; i < j; i++)
   {
      char asc = text[i] - key;
      printf("Text character is: %c  Encrypted character is: %c  Address of encrypted character is: %p: \n", text[i], asc, &asc);
      textEnc[i] = &asc;
      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    */
   }

   printf("Following is the resulting string: %s\n", textEnc);


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

craig@Vera:~/C_Programs/Console/CS_Encrypt/bin/Release$ ./CS_Encrypt 
String to encrpyt: Hello
Enter key [1 - 15]: 2
Text character is: H  Encrypted character is: F  Address of encrypted character is: 0x7ffc249e766b: 
textEnc[i] contains: k which has an integer value of: 107
Text character is: e  Encrypted character is: c  Address of encrypted character is: 0x7ffc249e766b: 
textEnc[i] contains: k which has an integer value of: 107
Text character is: l  Encrypted character is: j  Address of encrypted character is: 0x7ffc249e766b: 
textEnc[i] contains: k which has an integer value of: 107
Text character is: l  Encrypted character is: j  Address of encrypted character is: 0x7ffc249e766b: 
textEnc[i] contains: k which has an integer value of: 107
Text character is: o  Encrypted character is: m  Address of encrypted character is: 0x7ffc249e766b: 
textEnc[i] contains: k which has an integer value of: 107
Following is the resulting string: kkkkk
craig@Vera:~/C_Programs/Console/CS_Encrypt/bin/Release$ ./CS_Encrypt 
String to encrpyt: Hello
Enter key [1 - 15]: 2
Text character is: H  Encrypted character is: F  Address of encrypted character is: 0x7ffdbc272aab: 
textEnc[i] contains: � which has an integer value of: -85
Text character is: e  Encrypted character is: c  Address of encrypted character is: 0x7ffdbc272aab: 
textEnc[i] contains: � which has an integer value of: -85
Text character is: l  Encrypted character is: j  Address of encrypted character is: 0x7ffdbc272aab: 
textEnc[i] contains: � which has an integer value of: -85
Text character is: l  Encrypted character is: j  Address of encrypted character is: 0x7ffdbc272aab: 
textEnc[i] contains: � which has an integer value of: -85
Text character is: o  Encrypted character is: m  Address of encrypted character is: 0x7ffdbc272aab: 
textEnc[i] contains: � which has an integer value of: -85
Following is the resulting string: �����


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

for (int i = 0, j = length; i < j; i++)
   {
      char asc = text[i] - key;
      printf("Text character is: %c  Encrypted character is: %c  Address of encrypted character is: %p: \n", text[i], asc, &asc);
      textEnc[i] = asc;  /* Refactored */
      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    */
   }

   printf("Following is the resulting string: %s\n", textEnc);


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

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


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

相关问题