[英]subscripted value is neither array nor pointer nor vector
I have a Uni project that requires us to create a Ceaear Cipher program and we have to do it in C. I have learnt Java but not C and as a result I have had to learn C in 4 weeks. 我有一个Uni项目,要求我们创建一个Ceaear Cipher程序,我们必须用C来做。我学过Java,但没有学习C,因此我必须在4周内学习C。
Anyway I'm trying to combine 2 char arrays together in the second for loop and then after that add the alphabet after the code word, when I try to do this it keeps giving me the error that I have posted this topic as "subscripted value is neither array nor pointer nor vector". 无论如何,我试图在第二个for循环中将2个char数组组合在一起,然后在代码字后添加字母,当我尝试执行此操作时,它总是给我错误,我已将该主题发布为“下标值”既不是数组,也不是指针,也不是向量”。 I can easily do this in Java but C is a little more complicated to me.
我可以用Java轻松地做到这一点,但是C对我来说有点复杂。
Is there just an easier way to do this or am I missing something all together. 有没有更简单的方法可以做到这一点,或者我是否全都错过了一些东西。 Any help is appreciated.
任何帮助表示赞赏。
int main(){
char mainAlphabet;
char *mainPointer = &mainAlphabet;
char codeWord[20];
char codeAndAlphabet;
int i=0;
printf("Please enter a code word: ");
scanf("%s", &codeWord);
for(mainAlphabet=97 ; mainAlphabet<=122 ; mainAlphabet++){
//printf("%c", mainAlphabet);
}
for(i=0 ; i < sizeof(codeWord); i++){
codeAndAlphabet[i] = codeWord[i];
}
printf("%s \n", codeWord);
}
The error occurs because codeAndAlphabet
is a char
: 发生错误是因为
codeAndAlphabet
是一个char
:
char codeAndAlphabet;
Note that it's just a single char
, not an array of char
or a pointer to char
. 请注意,这只是一个单一的
char
,而不是一个数组char
或指向char
。 So, now, the left-hand side of: 因此,现在,左侧:
codeAndAlphabet[i] = codeWord[i]
is wrong because it's trying to use the subscripting (or indexing) syntax [i]
with something that isn't subscriptable, which is exactly what your compiler error message said. 这是错误的,因为它试图将下标(或索引)语法
[i]
与无法下标的内容一起使用,这恰好是您的编译器错误消息所说的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.