错误:不兼容的整数到指针转换分配给字符串' (aka' char *')来自' int' c
这是不完整的代码,但我正在尝试制作大写和小写的argv [1]的副本,但要获取错误消息。如果这是一种效率低下的方法,或者通常是完全错误的,我会很感激任何提示,但是在这种特定情况下,有没有办法解决错误?
我可以通过这个问题来确定编码的新手,所以很抱歉,如果这是一个愚蠢的问题,但是错误会在哪里发生呢?我意识到Argv [1]正在转换为整数,但我既不知道在哪里也不知道它如何修复它。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
string keyu[26];
string keyl[26];
string key = argv[1];
for (int u = 0; u < strlen(key); u++)
{
keyu[u] = toupper(key[u]);
}
for (int l = 0; l < strlen(key); l++)
{
keyl[l] = tolower(key[l]);
}
它给出的错误是:
14:17: error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion]
keyu[u] = toupper(key[u]);
This is incomplete code but I'm trying to make copies of argv[1] that are uppercase and lowercase, but get an error message. If it's an inefficient way to go about it in general or completely wrong I would appreciate any tips, but is there a way to fix the error in this specific case?
I'm extremely new to coding as you can tell by the question, so I'm sorry if it's a stupid one, but where might the error be occurring? I realize somehow argv[1] is being converted into an integer but I neither know where nor how to really fix it.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
string keyu[26];
string keyl[26];
string key = argv[1];
for (int u = 0; u < strlen(key); u++)
{
keyu[u] = toupper(key[u]);
}
for (int l = 0; l < strlen(key); l++)
{
keyl[l] = tolower(key[l]);
}
The error it gives out is:
14:17: error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int' [-Werror,-Wint-conversion]
keyu[u] = toupper(key[u]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些声明
等同于
这些声明,它们是指针的数组。
在这些语句中,
您正在尝试将整数(例如
toupper(key [u])
)分配给指针(例如keyu [u]
)看来您实际上要声明字符数组
要注意的是,您应该将终止零字符
'\ 0'
在for循环之后附加阵列,以确保数组包含字符串。These declarations
are equivalent to
That is they are arrays of pointers.
This in these statements
you are trying to assign an integer (as for example
toupper(key[u])
) to a pointer (as for examplekeyu[u]
)It seems you want actually to declare arrays of characters
Pay attention to that you should append the arrays with the terminating zero character
'\0'
after the for loops to guarantee that the arrays contain strings.