c- 在函数中分配结构成员时出错
c- Error when assigning structure member in function
我正在用 C 编写一个程序来查找塞塞密码中的移位。
作为其中的一部分,我首先对要解密的消息执行所有可能的移位,0-26,我使用一个结构来存储移位和消息。为此,我将一个结构作为指针传递给了一个函数。但是,当我尝试将结构的消息成员更改为解密消息时,我收到错误消息:行 'strcpy(s->message, cipherText);'.[=11 上的 '->' (have 'int') 无效类型参数=]
在函数中,我还为结构成员分配了一个局部变量,这工作正常。
代码:
#include <stdio.h>
#include <string.h>
#define ENCRYPT 0
#define DECRYPT 1
struct Solution {
int key;
char message[];
};
void Ceaser(struct Solution *s, char cipherText[], int mode);
void main(){
struct Solution solutions[26];
char cipherText[] = "lipps, asvph.";
for (int i = 0; i <= 26; ++i) {
solutions[i].key = i;
Ceaser(&solutions[i], cipherText, DECRYPT);
printf("Key: %d\tPlain text: %s\n", solutions[i].key,
solutions[i].message);
}
}
void Ceaser(struct Solution *s, char cipherText[], int mode) {
int len = strlen(cipherText);
int c;
int key = s->key;
for (int s = 0; s <= 26; ++s) {
if (mode == DECRYPT) {
key *= -1;
}
for (int i = 0; i < len; ++i) {
c = cipherText[i];
if (c >= 'A' && c <= 'Z') {
cipherText[i] = 'A' + ((c + key - 'A') % 26);
} else if (c >= 'a' && c <= 'z') {
cipherText[i] = 'a' + ((c + key - 'a') % 26);
}
}
//Error occurs below
strcpy(s->message, cipherText);
}
}
问题是您没有正确关闭 for(int s=...
,编译器认为 s->
您指的是循环变量 s
而不是 Solution* s
函数参数。
这就是您收到 无效类型 错误的原因。
以下是固定的(缩进更好的)版本:
void Ceaser(struct Solution *s, char cipherText[], int mode) {
int len = strlen(cipherText);
int c;
int key = s->key;
for (int s = 0; s <= 26; ++s) {
if (mode == DECRYPT) {
key *= -1;
}
for (int i = 0; i < len; ++i) {
c = cipherText[i];
if (c >= 'A' && c <= 'Z') {
cipherText[i] = 'A' + ((c + key - 'A') % 26);
} else if (c >= 'a' && c <= 'z') {
cipherText[i] = 'a' + ((c + key - 'a') % 26);
}
}
} //<--------was missing
strcpy(s->message, cipherText);
}
如果您让编译器警告 -Wshadow
起作用,您将获得非常有用的信息。
g++
test.cpp:65:30: note: shadowed declaration is here
void Ceaser(struct Solution *s, char cipherText[], int mode) {
clang++
note: previous declaration is here
void Ceaser(struct Solution *s, char cipherText[], int mode) {
icpc
warning #1599: declaration hides parameter "s" (declared at line 65)
for (int s = 0; s <= 26; ++s) {
void Ceaser(struct Solution *s, char cipherText[], int mode){
....
for (int s = 0; s <= 26; ++s){
你看不出明显的冲突吗——你两次使用了同一个变量名。 int s
将在 for 循环范围内覆盖先前声明的 s
,因此您的代码将无法与先前声明的代码进行交互。
把第一个s
改成一个合适的变量名(即"solution"),这样就避免了冲突,而且变量的用途也一目了然。单字符变量不是很清楚它们的用途,即使它们只是用于 for 循环。
我正在用 C 编写一个程序来查找塞塞密码中的移位。
作为其中的一部分,我首先对要解密的消息执行所有可能的移位,0-26,我使用一个结构来存储移位和消息。为此,我将一个结构作为指针传递给了一个函数。但是,当我尝试将结构的消息成员更改为解密消息时,我收到错误消息:行 'strcpy(s->message, cipherText);'.[=11 上的 '->' (have 'int') 无效类型参数=]
在函数中,我还为结构成员分配了一个局部变量,这工作正常。
代码:
#include <stdio.h>
#include <string.h>
#define ENCRYPT 0
#define DECRYPT 1
struct Solution {
int key;
char message[];
};
void Ceaser(struct Solution *s, char cipherText[], int mode);
void main(){
struct Solution solutions[26];
char cipherText[] = "lipps, asvph.";
for (int i = 0; i <= 26; ++i) {
solutions[i].key = i;
Ceaser(&solutions[i], cipherText, DECRYPT);
printf("Key: %d\tPlain text: %s\n", solutions[i].key,
solutions[i].message);
}
}
void Ceaser(struct Solution *s, char cipherText[], int mode) {
int len = strlen(cipherText);
int c;
int key = s->key;
for (int s = 0; s <= 26; ++s) {
if (mode == DECRYPT) {
key *= -1;
}
for (int i = 0; i < len; ++i) {
c = cipherText[i];
if (c >= 'A' && c <= 'Z') {
cipherText[i] = 'A' + ((c + key - 'A') % 26);
} else if (c >= 'a' && c <= 'z') {
cipherText[i] = 'a' + ((c + key - 'a') % 26);
}
}
//Error occurs below
strcpy(s->message, cipherText);
}
}
问题是您没有正确关闭 for(int s=...
,编译器认为 s->
您指的是循环变量 s
而不是 Solution* s
函数参数。
这就是您收到 无效类型 错误的原因。
以下是固定的(缩进更好的)版本:
void Ceaser(struct Solution *s, char cipherText[], int mode) {
int len = strlen(cipherText);
int c;
int key = s->key;
for (int s = 0; s <= 26; ++s) {
if (mode == DECRYPT) {
key *= -1;
}
for (int i = 0; i < len; ++i) {
c = cipherText[i];
if (c >= 'A' && c <= 'Z') {
cipherText[i] = 'A' + ((c + key - 'A') % 26);
} else if (c >= 'a' && c <= 'z') {
cipherText[i] = 'a' + ((c + key - 'a') % 26);
}
}
} //<--------was missing
strcpy(s->message, cipherText);
}
如果您让编译器警告 -Wshadow
起作用,您将获得非常有用的信息。
g++
test.cpp:65:30: note: shadowed declaration is here
void Ceaser(struct Solution *s, char cipherText[], int mode) {
clang++
note: previous declaration is here
void Ceaser(struct Solution *s, char cipherText[], int mode) {
icpc
warning #1599: declaration hides parameter "s" (declared at line 65)
for (int s = 0; s <= 26; ++s) {
void Ceaser(struct Solution *s, char cipherText[], int mode){
....
for (int s = 0; s <= 26; ++s){
你看不出明显的冲突吗——你两次使用了同一个变量名。 int s
将在 for 循环范围内覆盖先前声明的 s
,因此您的代码将无法与先前声明的代码进行交互。
把第一个s
改成一个合适的变量名(即"solution"),这样就避免了冲突,而且变量的用途也一目了然。单字符变量不是很清楚它们的用途,即使它们只是用于 for 循环。