C 中的本地作用域
Local scope in C
我在 C:
中有类似的东西
string getCipherText(string text, int key) {
string cipherText = "";
printf("Plaintext: %s, key: %i\n", text, key);
key = key % 26;
for (int i = 0; i < strlen(text); i++) {
if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z')) {
text[i] = (int) text[i] + key;
}
cipherText += text[i];
}
return cipherText;
}
为什么我返回的密文字符串是空的? for循环中不是同一个变量吗?来自 EdX https://ide.cs50.io where they have a string type in cs50.h
的云 IDE
假设string
是char*
的别名,cipherText += text[i];
不是连接字符串而是移动指针。
您应该像这样分配一个缓冲区并将结果存储在那里:
string getCipherText(string text, int key) {
size_t len = strlen(text):
string cipherText = malloc(len + 1);
if (cipherText == NULL) {
fputs("malloc() failed!\n", stderr);
return NULL;
}
printf("Plaintext: %s, key: %i\n", text, key);
key = key % 26;
for (int i = 0; i < len; i++) {
if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z')) {
text[i] = (int) text[i] + key;
}
cipherText[i] = text[i];
}
cipherText[len] = '[=10=]';
return cipherText;
}
如果这是 C,那么 C 在语言中没有类型 string
。
它可以通过typedef
或其他“技巧”创建。
但是该语言 不 支持使用 +=
将字符串连接在一起,如下所示:
cipherText += text[i];
由于您是直接修改“text”参数的内容,您可以删除对“ciphertext”变量的所有引用,并且
return text;
最后。这样做的好处是“文本”字符串已经存在于调用函数中,并且肯定在该调用函数的范围内。
我假设名称 string
表示以下 typedef 名称
typedef char *string;
因此在这个声明中
string cipherText = "";
声明了一个指向字符串文字的指针 ""
。
所以在这个声明中
cipherText += text[i];
指向字符串文字 ""
的指针使用指针算法递增 text[i]
的整数值。那就是指向nowhere.There的指针不是指针指向的有效对象。结果,该函数调用了未定义的行为。
这个语句还有一个bug
text[i] = (int) text[i] + key;
因为如果右侧的表达式表现为 signed char 类型,它可能会导致 char 类型溢出。
无论如何,实现与函数声明不对应。
函数声明意味着必须在不创建任何其他字符数组的情况下“就地”更改传递的字符串。
否则,函数参数应声明为 const char *text
,因为在创建原始字符串的修改副本时,原始字符串本身不会被修改。
我在 C:
中有类似的东西string getCipherText(string text, int key) {
string cipherText = "";
printf("Plaintext: %s, key: %i\n", text, key);
key = key % 26;
for (int i = 0; i < strlen(text); i++) {
if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z')) {
text[i] = (int) text[i] + key;
}
cipherText += text[i];
}
return cipherText;
}
为什么我返回的密文字符串是空的? for循环中不是同一个变量吗?来自 EdX https://ide.cs50.io where they have a string type in cs50.h
的云 IDE假设string
是char*
的别名,cipherText += text[i];
不是连接字符串而是移动指针。
您应该像这样分配一个缓冲区并将结果存储在那里:
string getCipherText(string text, int key) {
size_t len = strlen(text):
string cipherText = malloc(len + 1);
if (cipherText == NULL) {
fputs("malloc() failed!\n", stderr);
return NULL;
}
printf("Plaintext: %s, key: %i\n", text, key);
key = key % 26;
for (int i = 0; i < len; i++) {
if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z')) {
text[i] = (int) text[i] + key;
}
cipherText[i] = text[i];
}
cipherText[len] = '[=10=]';
return cipherText;
}
如果这是 C,那么 C 在语言中没有类型 string
。
它可以通过typedef
或其他“技巧”创建。
但是该语言 不 支持使用 +=
将字符串连接在一起,如下所示:
cipherText += text[i];
由于您是直接修改“text”参数的内容,您可以删除对“ciphertext”变量的所有引用,并且
return text;
最后。这样做的好处是“文本”字符串已经存在于调用函数中,并且肯定在该调用函数的范围内。
我假设名称 string
表示以下 typedef 名称
typedef char *string;
因此在这个声明中
string cipherText = "";
声明了一个指向字符串文字的指针 ""
。
所以在这个声明中
cipherText += text[i];
指向字符串文字 ""
的指针使用指针算法递增 text[i]
的整数值。那就是指向nowhere.There的指针不是指针指向的有效对象。结果,该函数调用了未定义的行为。
这个语句还有一个bug
text[i] = (int) text[i] + key;
因为如果右侧的表达式表现为 signed char 类型,它可能会导致 char 类型溢出。
无论如何,实现与函数声明不对应。
函数声明意味着必须在不创建任何其他字符数组的情况下“就地”更改传递的字符串。
否则,函数参数应声明为 const char *text
,因为在创建原始字符串的修改副本时,原始字符串本身不会被修改。