C语言 赋值从整数生成指针,没有强制转换和其他问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16587672/
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 06:21:52  来源:igfitidea点击:

Assignment makes pointer from integer without a cast and other questions

cpointerscasting

提问by Indigo

I just started learning C a few days ago and I'm having a bit of difficulties with pointers. I'm trying to convert a string to an array of integers. The little snipet below seems to be working but I'm getting a warning :

几天前我刚刚开始学习 C,我在使用指针时遇到了一些困难。我正在尝试将字符串转换为整数数组。下面的小 snipet 似乎正在工作,但我收到警告:

In function 'charToInt32' warning: assignment makes pointer from integer without a cast [enabled by default]| ||=== Build finished: 0 errors, 1 warnings (0 minutes, 0 seconds) ===|

在函数 'charToInt32' 中警告:赋值使指针从整数而不进行强制转换 [默认启用]| ||=== 构建完成:0 个错误,1 个警告(0 分钟,0 秒)===|

The warning comes from the line

警告来自行

int32result[pos] = returnedInteger;

So I'm trying to understand what's the best solution. Should I use strncpy (but can I use strncpy for integers?) or something else or did I just completly misunderstand pointers?

所以我试图了解什么是最好的解决方案。我应该使用 strncpy (但我可以将 strncpy 用于整数吗?)或其他东西还是我完全误解了指针?

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


    int charToInt32(char * clearText, unsigned int * int32result[])
    {
        int i = 0, j = 0, pos = 0;          /* Counters */
        int dec[4] = {24, 16, 8, 0};        /* Byte positions in an array*/
        unsigned int returnedInteger = 0;           /*   so we can change the order*/ 

        for (i=0; clearText[i]; i+=4) {
            returnedInteger = 0;
            for (j=0; j <= 3 ; j++) {
                returnedInteger |= (unsigned int) (clearText[i+j] << dec[j]) ;
            }

            int32result[pos] = returnedInteger;
            pos++;
        }
        return pos;
    }



    int main()
    {
        int i = 0;
        unsigned int * int32result[1024] = {0};
        char * clearText =  "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        printf(">>Clear: %s\n", clearText);
        charToInt32(clearText, int32result); // Do the conversion to int32.
        printf(">>Int32 converted: ");
        for (i=0; int32result[i]; i++)
            printf("%u ", (unsigned int) int32result[i]);
        printf("\n");
        return 0;
    }

Also, at the end of the program I have the following line:

此外,在程序结束时,我有以下行:

printf("%u ", (unsigned int) int32result[i])

Casting int32result[i] to unsigned int is it the only solution to avoid another warning of using %u for an unsigned int * ?

将 int32result[i] 转换为 unsigned int 是避免将 %u 用于 unsigned int * 的另一个警告的唯一解决方案吗?

I did check the other "assignment makes integer from pointer without cast" topics/question but I could not quite get a final answer from them.

我确实检查了另一个“没有强制转换的指针的赋值使整数”主题/问题,但我无法从他们那里得到最终答案。

Thank-you for your help.

感谢您的帮助。

回答by simonc

unsigned int * int32result[1024]

declares an array of 1024 unsigned intpointers. I think you want an array of ints here instead

声明一个包含 1024 个unsigned int指针的数组。我认为你在这里想要一个整数数组

unsigned int int32result[1024]

You have a similar problem in charToInt32where the unsigned int * int32result[]argument specifies an array of unsigned intarrays. You have a single array so could pass unsigned int * int32resultinstead (i.e. remove the []).

charToInt32unsigned int * int32result[]参数指定数组数组时遇到了类似的问题unsigned int。您有一个数组,因此可以unsigned int * int32result改为传递(即删除[])。

The rest of your code should just work then. Calls like

您的其余代码应该可以正常工作。像这样的电话

charToInt32(clearText, int32result);

are equivalent to

相当于

charToInt32(clearText, &int32result[0]);

回答by Daniel Fischer

You declare charToInt32to take a pointer-to-pointer-to-unsigned intas an argument,

您声明charToInt32将指向指针的指针unsigned int作为参数,

int charToInt32(char * clearText, unsigned int * int32result[])

but you use the argument as an array of unsigned int, so the argument should be

但是您将参数用作 的数组unsigned int,因此该参数应该是

unsigned int *int32result

or equivalently (in a function declaration!! Not in general)

或等效地(在函数声明中!不是一般的)

unsigned int int32result[]

And in main, it should be

而在main,它应该是

unsigned int int32result[1024] = {0};

there you also have one level of pointers too many.

在那里你也有太多的指针级别。

回答by Rerito

The warning you get comes from the fact that you hare assigning an integer value to a pointer.

您收到的警告来自您将整数值分配给指针的事实。

/* int32result is an array of POINTERS */
unsigned int * int32result[];
/* But returnedInteger is an int */
unsigned int returnedInteger = 0;
/*...*/
/* int32result[pos] is a pointer to an integer ...
 * Thus here, you assign a "pure" integer to a pointer,
 * hence the warning */
int32result[pos] = returnedInteger;

It is also totally sane that the compiler raises a warning when hitting the printf if you do not cast the value. As a matter of fact, pointers on common machines are often 32 or 64 bits, which is enough to avoid information loss during the faultyassignment. That is why your print statement should look like the program works as expected.

如果您不强制转换值,编译器在点击 printf 时会发出警告也是完全正常的。事实上,普通机器上的指针往往是32位或64位,这足以避免错误赋值时信息丢失。这就是为什么您的打印语句应该看起来像程序按预期工作的原因。

回答by parkydr

unsigned int * int32result[] 

is an array of unsigned intpointers, so you are assigning the value to a pointer.

是一个unsigned int指针数组,因此您将值分配给一个指针。