c - C在变量范围改变时给出奇怪的输出
问题描述
当我将nonAlphaCount声明放在 for 循环之上时,C 会做一些令人毛骨悚然的事情。我无法解释为什么输出不同。
对于版本 1(主方法上方的 int 声明),我的输入输出是:输入:./Vigenere.exe bacon 输入纯文本:上午 11 点在公园见我输出:Negh zf av huf pcfx bt gzrwep oz
对于版本 2(上面的 int 声明 for loop) 输入:./Vigenere.exe bacon 输入纯文本:上午 11 点在公园见我 输出:NRQ Q M
[L \M^^ K
QXXZQZ M
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
const int INPUT_LEN = 255;
const int ALPHABET_LEN = 26;
int nonAlphaCount = 0;
int main (int count, char *args[])
{
char plainText[INPUT_LEN];
char *cipherText;
char *keyWord;
if ( count < 2 || count > 2)
{
printf("There is no key");
return 1;
}
strcpy(keyWord, args[1]);
int keyWord_LEN = strlen(keyWord);
printf("Enter plain text: ");
fgets (plainText, INPUT_LEN, stdin);
int strLength = strlen(plainText);
cipherText = malloc(strLength);
printf("%s", plainText);
for (int i = 0; i < strLength; i++ ){
if(plainText[i] == '\0' || plainText[i] == '\n'|| plainText[i] == '\r')
break;
if(isalpha(plainText[i]))
{
// Default lower
int asciiUpperOrLower = 97;
int keyUpperOrLower = 97;
if(isupper(plainText[i]))
asciiUpperOrLower = 65;
if(isupper(keyWord[i % keyWord_LEN]))
keyUpperOrLower = 65;
int Key = keyWord[(i - nonAlphaCount) % keyWord_LEN] - keyUpperOrLower;
int alphabetBaseletter = ((plainText[i] - asciiUpperOrLower + Key) % ALPHABET_LEN);
cipherText[i] = alphabetBaseletter + asciiUpperOrLower;
}
else{
cipherText[i] = plainText[i];
nonAlphaCount++;
}
}
// Set string terminator.
cipherText[strLength - 1] = '\0' ;
printf("%s", cipherText);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
const int INPUT_LEN = 255;
const int ALPHABET_LEN = 26;
int main (int count, char *args[])
{
char plainText[INPUT_LEN];
char *cipherText;
char *keyWord;
if ( count < 2 || count > 2)
{
printf("There is no key");
return 1;
}
strcpy(keyWord, args[1]);
int keyWord_LEN = strlen(keyWord);
printf("Enter plain text: ");
fgets (plainText, INPUT_LEN, stdin);
int strLength = strlen(plainText);
cipherText = malloc(strLength);
printf("%s", plainText);
**int nonAlphaCount = 0;**
for (int i = 0; i < strLength; i++ ){
if(plainText[i] == '\0' || plainText[i] == '\n'|| plainText[i] == '\r')
break;
if(isalpha(plainText[i]))
{
// Default lower
int asciiUpperOrLower = 97;
int keyUpperOrLower = 97;
if(isupper(plainText[i]))
asciiUpperOrLower = 65;
if(isupper(keyWord[i % keyWord_LEN]))
keyUpperOrLower = 65;
int Key = keyWord[(i - nonAlphaCount) % keyWord_LEN] - keyUpperOrLower;
int alphabetBaseletter = ((plainText[i] - asciiUpperOrLower + Key) % ALPHABET_LEN);
cipherText[i] = alphabetBaseletter + asciiUpperOrLower;
}
else{
cipherText[i] = plainText[i];
nonAlphaCount++;
}
}
// Set string terminator.
cipherText[strLength - 1] = '\0' ;
printf("%s", cipherText);
return 0;
}
解决方案
这两个程序都表现出未定义的行为
char *keyWord;
...
strcpy(keyWord, args[1]);
编译器警告揭示了这一点:“使用了未初始化的局部变量'keyWord'”。您尚未分配任何内存。
如果其中一个程序碰巧有效,那就这样吧。
推荐阅读
- python - 部署项目时出现迁移错误
- mongodb - 聚合以按特定日期查找文档
- javascript - 当ul变得大于屏幕尺寸时需要在ul上添加overflow-x滚动
- android-layout - 约束布局孩子离开屏幕
- ios - 静态放置的单行表格视图
- javascript - 事件委托帮助,我怎样才能只使用纯 Javascript 定位标签?
- mysql - SQL 中每个账户的前 2 名商户
- django - Django Rest Framework:视图集或序列化程序中需要外键字段
- android - Android 模拟器不适用于 Apple M1 芯片
- powershell - CMD 或 Powershell 命令组合(合并)来自两个不相等文件的行