C语言 *初学者* C:不兼容的整数到指针转换将“char”传递给“const char *”类型的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19873562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:55:41  来源:igfitidea点击:

*Beginner* C: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'

ccs50

提问by Ting

I'm trying to convert each letter in a string to it's ASCII number. Using

我正在尝试将字符串中的每个字母转换为它的 ASCII 数字。使用

 int letter = (atoi(ptext[i]));

gives me this error:

给我这个错误:

error: incompatible integer to pointer conversion
  passing 'char' to parameter of type 'const char *'; take the
  address with & [-Werror]
    int letter = (atoi(ptext[i]));
                       ^~~~~~~~
                       &
/usr/include/stdlib.h:148:32: note: passing argument to parameter
      '__nptr' here
extern int atoi (__const char *__nptr)

Here is some of the rest of my code that may be relevant:

以下是我的其他一些可能相关的代码:

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

int main(int argc, string argv[])
{

    printf("Give a string to cipher:\n");
    string ptext = GetString();

    int i = 0;

    if (isupper(ptext[i]))
    {
        int letter = (atoi(ptext[i]));
    }
}

What am I doing wrong, and how do I fix this so I can convert a string into ASCII values?

我做错了什么,我该如何解决这个问题,以便将字符串转换为 ASCII 值?

Note: cs50.hlets me use "string" instead of "char*" in main.

注意:cs50.h让我在 main 中使用“ string”而不是“ char*”。

采纳答案by Artur

You do not need to convert characters to a number. It is a matter of interpretation of your data.

您不需要将字符转换为数字。这是对您的数据的解释问题。

Charater 'A' can be thought of 0x41 or 65 so this is perfectly fine:

字符 'A' 可以被认为是 0x41 或 65,所以这完全没问题:

int number = 'A';

and value on variable number is 0x41/65 or 1000001b depending how you want to present it/treat it.

变量编号的值是 0x41/65 或 1000001b,具体取决于您希望如何呈现/处理它。

As for interpretation : 0xFF may be treated as 255 if you present it as unsigned value or even as -1 when treated as signed and kept in 8 bits.

至于解释:如果将 0xFF 表示为无符号值,则可能将其视为 255,或者将其视为有符号并保留为 8 位时甚至视为 -1。

So your question:

所以你的问题:

can convert a string into ASCII values?

可以将字符串转换为 ASCII 值吗?

is kind of wrong - all characters of the string are already ascii values - it is just a matter of how you treat/print/interpret/present them.

有点错误 - 字符串的所有字符都已经是 ascii 值 - 这只是你如何对待/打印/解释/呈现它们的问题。

回答by

atoi()expects a string. You only want the char code of the character... which is the character itself,since in C, the charis a normal integer type just like every other integer, and a string is an array of chars which hold the character codes themselves.

atoi()期待一个字符串。您只需要字符的 char 代码......这是字符本身,因为在 C 中, thechar是一个普通的整数类型,就像其他所有整数一样,而字符串是一个chars数组,其中包含字符代码本身。

Consequently,

最后,

int letter = ptext[i];

would do the job.

会做的工作。

回答by Gangadhar

int letter = (atoi(ptext[i]));

atoi()convert string to integer not character.

atoi()将字符串转换为整数而不是字符。

To store ascii value of character into integer variable Do direct assignment of character to integer variable.

将字符的ascii 值存入整型变量 将字符直接赋值给整型变量。

int letter = ptext[i];