Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不丢失信息的情况下将void*转换为int(在c中)?_C_Type Conversion - Fatal编程技术网

如何在不丢失信息的情况下将void*转换为int(在c中)?

如何在不丢失信息的情况下将void*转换为int(在c中)?,c,type-conversion,C,Type Conversion,如果void*包含可变数量的信息,那么仅仅将其存储在整数中不可能丢失信息吗?信息不应该存储在多个整数(也称为int*)中吗 例: 如果info=Absdfsdfskewlrew,num不能正确保存此信息吗 将指针传递给int变量 如果整数的大小不大于指针的大小,大多数实现都会将int转换为指针并返回。它是IMO UB,但我知道的实现将以您认为的方式编译和执行它void*有时用于向函数传递指向“任何类型”的值的指针 您还需要另一个参数来描述void*实际指向的对象,以便将其转换为正确的指针类型 以

如果void*包含可变数量的信息,那么仅仅将其存储在整数中不可能丢失信息吗?信息不应该存储在多个整数(也称为int*)中吗

例:

如果info=Absdfsdfskewlrew,num不能正确保存此信息吗

  • 将指针传递给int变量

  • 如果整数的大小不大于指针的大小,大多数实现都会将int转换为指针并返回。它是IMO UB,但我知道的实现将以您认为的方式编译和执行它

    void*
    有时用于向函数传递指向“任何类型”的值的指针

    您还需要另一个参数来描述void*实际指向的对象,以便将其转换为正确的指针类型

    以下示例说明了该概念:

    #include <stdio.h>
    
    typedef enum {
        what_int,
        what_float,
        what_string
    } what_t;
    
    void print_it(what_t what, void *value)
    {
        switch (what) {
            case what_int:
                printf("It is an int: %d\n", * (int*) value);
                break;
            case what_float:
                printf("It is a float: %f\n", * (float*) value);
                break;
            case what_string:
                printf("It is a string: %s\n", (char*) value);
                break;
            default:
                printf("I don't understand what it is\n");
        }
    }
    
    int main()
    {
        int i = 5;
        print_it(what_int, &i);
    
        float f = 3.1415;
        print_it(what_float, &f);
    
        char *s = "Hello world";
        print_it(what_string, s);
    
        return 0;
    }
    

    对在32位整数中不能存储超过32位的信息。但为什么要问这个问题?为什么要尝试“将
    void*
    转换为
    int
    ”?您正在尝试转换指针或指针指向的数据吗?您正在将
    info
    中存储的地址类型转换为int,而不是该地址中存储的数据。但是,将存储在
    void*
    中的地址强制转换为有符号整数类型的正确数据类型是
    intptr\t
    。有人告诉过你
    void*
    是C中的“泛型”类型吗?可以“保存”任何类型的数据?不完全是这样
    void*
    是一种通用指针类型,可以指向任何类型的数据。
    int func(void* info)
    {
       int num = *(int *)info;
       // or 
       memcpy(&num, info, sizeof(num));
    }
    
    
    //And usage
    
    int x = 6;
    func(&x);
    
    #include <stdio.h>
    
    typedef enum {
        what_int,
        what_float,
        what_string
    } what_t;
    
    void print_it(what_t what, void *value)
    {
        switch (what) {
            case what_int:
                printf("It is an int: %d\n", * (int*) value);
                break;
            case what_float:
                printf("It is a float: %f\n", * (float*) value);
                break;
            case what_string:
                printf("It is a string: %s\n", (char*) value);
                break;
            default:
                printf("I don't understand what it is\n");
        }
    }
    
    int main()
    {
        int i = 5;
        print_it(what_int, &i);
    
        float f = 3.1415;
        print_it(what_float, &f);
    
        char *s = "Hello world";
        print_it(what_string, s);
    
        return 0;
    }
    
    It is an int: 5
    It is a float: 3.141500
    It is a string: Hello world