小编use*_*208的帖子

在C中将数组的内容转换为算术类型

我将面对奇怪的行为,将数组中的单个项目(或更多甚至解除引用)转换为单个算术类型.

下面是一个简化的测试用例:

void test1()
{
    unsigned char test[10] = {0};
    unsigned long i=0xffffffff;

    *((unsigned long *)(&test[3])) = i;


    int it;

    for ( it = 0 ; it < 10 ; it++ )

    {
        printf("%02x ", test[it]);

    }
}
void test2()
{
    unsigned char test[10] = {0};
    unsigned char test2[10] = {0};
    test[2]=0xFF;
    test[3]=0xFF;

    *((unsigned short *)(&test2[1])) = *((unsigned short *)(&test[2]));


    int it;

    for ( it = 0 ; it < 10 ; it++ )

    {
        printf("%02x ", test2[it]);

    }
} …
Run Code Online (Sandbox Code Playgroud)

c pointers casting access-violation dereference

0
推荐指数
1
解决办法
129
查看次数

标签 统计

access-violation ×1

c ×1

casting ×1

dereference ×1

pointers ×1