错误:不兼容的整数到指针转换分配给字符串' (aka' char *')来自' int' c

发布于 2025-02-03 18:07:43 字数 819 浏览 2 评论 0原文

这是不完整的代码,但我正在尝试制作大写和小写的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2025-02-10 18:07:43

这些声明

string keyu[26];
string keyl[26];

等同于

char * keyu[26];
char * keyl[26];

这些声明,它们是指针的数组。

在这些语句中,

keyu[u] = toupper(key[u]);
keyl[l] = tolower(key[l]);

您正在尝试将整数(例如toupper(key [u]))分配给指针(例如keyu [u]

)看来您实际上要声明字符数组

char keyu[26];
char keyl[26];

要注意的是,您应该将终止零字符'\ 0'在for循环之后附加阵列,以确保数组包含字符串。

These declarations

string keyu[26];
string keyl[26];

are equivalent to

char * keyu[26];
char * keyl[26];

That is they are arrays of pointers.

This in these statements

keyu[u] = toupper(key[u]);
keyl[l] = tolower(key[l]);

you are trying to assign an integer (as for example toupper(key[u])) to a pointer (as for example keyu[u])

It seems you want actually to declare arrays of characters

char keyu[26];
char keyl[26];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文