gpt4 book ai didi

c - "Incompatible integer to pointer conversion"

转载 作者:太空宇宙 更新时间:2023-11-04 05:54:05 25 4
gpt4 key购买 nike

由于这些错误,以下程序拒绝编译:

vigenere.c:52:31: error: incompatible integer to pointer conversion assigning to
'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion]
...ciphertext[i] = ((((plaintext[i] - 65) + keyword[num % keylength]) % 26) + 65);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vigenere.c:56:31: error: incompatible integer to pointer conversion assigning to
'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion]
...ciphertext[i] = ((((plaintext[i] - 97) + keyword[num % keylength]) % 26) + 97);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

下面是程序,旨在实现一个简单的 vigenere 密码:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

int main(int argc, string argv[])
{
if(argc != 2)
{
printf("Invalid input; try again!\n");
return 1;
}
else if(argv[1])
{
for(int i = 0, n = strlen(argv[1]); i < n; i++)
{
if(!isalpha(argv[1][i]))
{
printf("Invalid input; try again!\n");
return 1;
}
}
}

// get plaintext from user
string plaintext = GetString();
string ciphertext[100];
int num = 0;

string keyword = argv[1];
int keylength = strlen(keyword);

// change key values from letters to shifts
for(int i = 0, n = keylength; i < n; i++)
{
if(isupper(keyword[i]))
{
keyword[i] = keyword[i] - 65;
}
else if(islower(keyword[i]))
{
keyword[i] = keyword[i] - 97;
}
}

for(int i = 0, n = strlen(plaintext); i < n; i++)
{
if(isalpha(plaintext[i]))
{
if(isupper(plaintext[i]))
{
ciphertext[i] = ((((plaintext[i] - 65) + keyword[num % keylength]) % 26) + 65);
}
else if(islower(plaintext[i]))
{
ciphertext[i] = ((((plaintext[i] - 97) + keyword[num % keylength]) % 26) + 97);
}
num++;
}
// non-alphabetic characters
else
{
ciphertext[i] = plaintext[i];
}
printf("%c", ciphertext[i]);
}
printf("\n");
}

我不知道为什么编译器会抛出错误,因为我有一个旧版本的程序,几个月前编译的(代码在第 52 行和第 56 行完全相同)工作正常。

我真的很感激任何和所有的帮助:)

最佳答案

变量ciphertextchar*的数组,我觉得应该是:

char ciphertext[1000]

关于c - "Incompatible integer to pointer conversion",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653813/

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