Cast of pointers to struct type

Source: Internet
Author: User
The struct body is the same type as int, and is a data type. Other types of conversion, how the structure is converted, there is no special place.
The landlord may want to know is not the structure of how to force the conversion of the problem, guess, the landlord would like to know the following several questions:
If you cast a struct type to another struct (or type), what happens to the members of the struct.
If you cast a struct-type to another struct (or type), then what is the value of the member of the struct?
Answer:
1, the essence of the structure is: we and C language agreed with the length of memory space, and its content arrangements. Suppose the following two structures:
struct A1
{
int A;
Char b;
};

struct A2
{
Char A;
int b;
};
Next, define the variable with struct A1 and struct A2, and assign the initial value:
struct A1 x = {, ' A '};
struct A2 y = {' A ', 10};
The most important thing now is to know the memory of X and Y:
X's memory arrangement is: Before 4B, after 1 B;
The memory arrangement of Y is: Before 1B, after 4 B.
If there is struct A2 z;
Z.A = ((struct A2) x). A;
So the C language will explain the space of X, according to the pattern of struct A2:

That is, the first byte of X is treated as the first member and the data is processed in ASCII code, and the following 4B is considered the second member, and the data is interpreted in the complement format.


End of this section

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

The different types of structure in C language can be forcibly converted between pointers, very free and dangerous. As long as you understand its internal mechanism, you will find that C is very flexible.

One.

The structure body declares how the memory is distributed,
The structure body pointer declares the first address of the structure body,
A struct member declares the offset address of the member in the structure body.

The value of the variable is stored in memory in binary form, each memory byte corresponds to a memory address, and the value of the memory store itself is not integer, pointer, character, etc., the difference exists because we have different interpretations of them, the value of Param is a 32-bit value, and stored in a memory unit, By using this 32-bit value, you can find the starting address of the structure to which Param refers, and refer to these variables by the starting address and by the offset from the starting address of the variables contained in each structure, param->bisdisable just the more readable wording of this reference, As long as Param is a pointer to Paint_param, then the Param value is sure to exist, param exists, the offset is known, then param->bisdisable must exist, just remember,param-> Bisdisable only represents the value of a certain offset to param.

Not that an address has that structure that you can reference, even if it's not, you can quote it, because you've already told the compiler that the param variable is a variable that points to a paint_param struct and indicates the value of the Param, and the machine code has no data structure in its eyes. It is only mechanically in accordance with the instructions required to take the value of the memory address, that just the example, the Peg->x,peg->y reference regardless of whether the 0x30000000 existence of an eg structure is legitimate, if the first 8 bytes of 0x30000000 began to exist in the eg structure, Then the reference is the value of the struct, if the position is undefined, then the result of the reference is undefined value in these 8 bytes, the memory location is always there, and the reference to the in-memory value is the value of the memory unit corresponding to the memory location.

As an example:


typedef STRUCT_EG
{
int x;
int y;
}eg;
int point = 0x30000000;
eg *peg = (eg*) point;

You can see that the point itself is just an integer variable, but such an assignment is legal, the value of Peg->x is four bytes starting with 0x30000000, and Peg->y is the four byte of the beginning of the 0x30000004
The value of the Pmsg->wparam is 0x30000000, which is PARAM, which points to a block of memory at the 0x30000000 address, which is distributed in a paint_param manner.


As an example:

typedef struct QUEUENODE
{
struct Queuenode * PNEXT;
}tqueuenode;

typedef struct QMSG
{
Tqueuenode Node;
TCHATSYSMSG data;

}tqmsg;

typedef struct CHATSYSMSG
{
int CONNFD;
Char Version;
Char Msgtype;
Char serialnumber;
int Msglen;
Char Msg[max_num_str];
}tchatsysmsg;

Casts between them:

Tqmsg * PTEMPMSG;
(Tqueuenode * *) &pTempMsg;
&pTempMsg->data     i.e. (tchatsysmsg *) &pTempMsg->data;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.