C 下标值不是数组、指针或向量 - 是吗?
Posted
技术标签:
【中文标题】C 下标值不是数组、指针或向量 - 是吗?【英文标题】:C Subscripted value is not an array, pointer, or vector - Yes it is? 【发布时间】:2016-06-13 04:05:14 【问题描述】:我构建了一个应用,它使用 Vigenere 密码对单词或句子进行编码。它可以工作,但所有内容都包含在 main() 中。我正在使用函数重建应用程序。但是当我认为它是一个数组时,我收到错误“下标值不是数组、指针或向量”。
错误由此而来
if ((int)sentencetoencode[repeatciphercounter] < 65 || ((int)sentencetoencode[repeatciphercounter] > 90 && (int)sentencetoencode[repeatciphercounter] < 97) || (int)sentencetoencode[repeatciphercounter] > 122)
ciphersentence[repeatciphercounter] = (char)32;
关于将“char [100]”传递给“char”参数类型的不兼容指针的附加警告
CipherRepeater(sentencetoencode, cipherwordtorepeat, ciphersentence);
完整代码
#include <stdio.h>
#include <string.h>
char CipherRepeater(char sentencetoencode, char cipherwordtorepeat, char ciphersentence);
int main(int argc, char * argv[])
// This is a message to encode using Vigenere cipher
char sentencetoencode[100] = "secret message is come over at five";
// Pass a word at command line, then assign that word to cipherwordtorepeat
// This word is the Vigenere encoding key
// Example word: cat
char cipherwordtorepeat[100];
for(unsigned long argvcounter = 0, argvlength = strlen(argv[1]); argvcounter < argvlength; argvcounter++)
cipherwordtorepeat[argvcounter] = argv[1][argvcounter];
// Now the word cat needs to be repeated until it matches the length of the message
// Example "secret message is come over at five"
// "catcat catcatc at catc atca tc atca"
// Declare an array of characters
// Then the function fills the array with the repeating word
char ciphersentence[100];
CipherRepeater(sentencetoencode, cipherwordtorepeat, ciphersentence);
printf("%s\n", ciphersentence);
return 0;
char CipherRepeater(char sentencetoencode, char cipherwordtorepeat, char ciphersentence)
// Create a counter to make sure to go back to char 0 in the word array when end is reached
int endofwordcounter = 0;
unsigned long cipherwordlength = strlen(&cipherwordtorepeat);
// Take the cipher word and repeat it until it's the same length as the word to encode
for (unsigned long repeatciphercounter = 0, sentencetoencodelength = strlen(&sentencetoencode); repeatciphercounter < sentencetoencodelength; repeatciphercounter++)
// Put a space in the new string in any spot where a non-letter is used
if ((int)sentencetoencode[repeatciphercounter] < 65 || ((int)sentencetoencode[repeatciphercounter] > 90 && (int)sentencetoencode[repeatciphercounter] < 97) || (int)sentencetoencode[repeatciphercounter] > 122)
ciphersentence[repeatciphercounter] = (char)32;
else
// Copy the character
ciphersentence[repeatciphercounter] = cipherwordtorepeat[endofwordcounter];
endofwordcounter++;
if (endofwordcounter >= cipherwordlength)
endofwordcounter = 0;
return 0;
【问题讨论】:
char cipherwordtorepeat
表示cipherwordtorepeat
是单个字符(因此不是数组、指针或向量)
你需要了解char
、char []
和char *
之间的区别。第一个是单个字符,而不是字符串。第二个是字符数组。第三个是指向字符的指针。指向的字符可能是字符数组的成员。 char []
可以转换为char *
,实际上是在表达式上下文中使用时。
【参考方案1】:
您的 sentencetoencode
声明为
// This is a message to encode using Vigenere cipher
char sentencetoencode[100] = "secret message is come over at five";
但您的 CipherRepeater
被声明为
char CipherRepeater(char sentencetoencode, char cipherwordtorepeat,
char ciphersentence);
接受char
而不是char*
。您正在将char*
传递给该函数
这是产生错误
incompatible pointer to integer conversion passing 'char [100]' to parameter type of 'char'
所以将char
更改为char*
。
并且还将strlen(&cipherwordtorepeat)
替换为strlen(cipherwordtorepeat)
,并与for 循环中的另一个strlen
类似。
【讨论】:
好的,有道理。但是在切换到 char* 后,我仍然遇到其中三个错误。都在上面相同的 if 语句中。 我的警告从 1 变为 5。同样的警告。 你能说出警告吗? 代码:CipherRepeater(sentencetoencode, cipherwordtorpeat, ciphersentence);提供指向将“char [100]”传递给参数类型“char”的整数转换的不兼容指针 代码:unsigned long cipherwordlength = strlen(cipherwordtorpeat);将不兼容的整数传递给指针转换,将“char”传递给“const char*”类型的参数以上是关于C 下标值不是数组、指针或向量 - 是吗?的主要内容,如果未能解决你的问题,请参考以下文章