gpt4 book ai didi

C 下标值不是数组、指针或 vector - 是吗?

转载 作者:行者123 更新时间:2023-11-30 20:04:41 29 4
gpt4 key购买 nike

我构建了一个使用维吉尼亚密码对单词或句子进行编码的应用程序。它可以工作,但所有内容都包含在 main() 中。我正在使用函数重建应用程序。但当我认为它是一个数组时,我收到错误“下标值不是数组、指针或 vector ”。

错误由此而来

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;
}

最佳答案

您的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 类似的操作。

关于C 下标值不是数组、指针或 vector - 是吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37781663/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com