简体   繁体   English

*((int *)(&val)+1)是什么意思

[英]What does this mean *((int*)(&val) +1)

I'm trying to understand this line of code. 我试图理解这一行代码。 Can someone help me? 有人能帮我吗? Is it saving the result in the variable val or in the address of the variable val? 是否将结果保存在变量val或变量val的地址中?

*((int*)(&val) +1)= A*(y) + (B - C)

Thank you 谢谢

&val take the address of val &valval地址
(int*)(&val) consider this address as a pointer to int (int*)(&val)将此地址视为指向int的指针
(int*)(&val) +1 increment this address by 1 (times sizeof(int) ) (int*)(&val) +1将该地址加1(乘以sizeof(int)倍数)
*((int*)(&val) +1) = ... assign the right hand side value at this incremented address *((int*)(&val) +1) = ...在此递增地址处分配右侧值

It is interpreting val as if it was an array of integers, and storing the result of the right hand expression in its second element. 它将val解释为好像是整数数组,并将右手表达式的结果存储在其第二个元素中。 To understand exactly the point of it all you should provide some more context (my guess: is it manipulating the raw content of double values?) 为了确切地理解所有内容,您应该提供更多上下文(我的猜测:它是在操纵double值的原始内容吗?)

Notice that, depending on the type of val , this may be undefined behavior due to strict aliasing rules. 请注意,由于严格的别名规则,根据val的类型,这可能是未定义的行为。

Divide expression *((int*)(&val) +1) into smaller ones to understand it: 将表达式*((int*)(&val) +1)分成较小的表达式以了解它:

  1. take address of val ( &val) and treat it as a pointer to an int (int *) 接受val&val)地址,并将其作为指向int (int *)的指针
  2. add 1 +1 to this pointer which means 'move pointer to next int' as it was an array of ints. 向此指针添加1 +1 ,这意味着“将指针移动到下一个int”,因为它是一个int数组。
  3. finaly by combining * and = apply right hand side expression to int pointed by pointer. 通过组合*=最终确定将右侧表达式应用于指针所指向的int

I hope others have answered your question. 希望其他人回答了您的问题。 Adding to what others have said, the same code can be written as follows: 除了其他人所说的,相同的代码可以编写如下:

(int*)(&val)[1]= A*(y) + (B - C)

where (int*) will type cast &val as an implicit pointer to an integer which points to the address of val and [1] indicates the first integer location ahead of the location where val is stored. 其中(int *)将类型转换&val作为指向整数的隐式指针,该整数指向val的地址,[1]指示存储val的位置之前的第一个整数位置。

This is how arrays are interpreted. 这就是数组的解释方式。 Say you have an array 说你有一个数组

int a[10];

For this array, 'a' is a pointer which points to the base address ( address of the element a[0] ), and a[i] is nothing but *(a+i) , ie the element which is i locations ahead of the first element of the array. 对于此数组, “ a”是一个指向基地址(元素a [0]的地址)的指针,而a [i]除了*(a + i)就是什么,即位于i前面的元素数组的第一个元素。

This is not correct code and you should never use it 这是不正确的代码,您永远不要使用它

Imagine this class: 想象一下这堂课:

class A {
    int number = 10;
    public:
    void print(){ std::cout << number; }
};

The int number is private for the access not the use! int number专用于访问,不能使用!

So how can we access this private int . 那么我们如何访问这个私有的int

Simply: 只是:

A obj;
*( (int*) ( &obj ) ) = 100;
obj.print();

output 输出

100 100


demo 演示

Now if you would have more than one data then how to access? 现在,如果您将有多个数据,那么如何访问?

by this syntax: 通过这种语法:
*((int*)(&val) +1)

It says: 它说:
find the address of the first data, 找到第一个数据的地址,
one index go ahead, 一个指标继续前进,
cast it to the int* , 将其转换为int*
then dereference it, 然后取消引用
then initialize it 然后初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM