C Language pointer type casting

Source: Internet
Author: User
Tags type casting

About the C language pointer type cast reference an article:

In C, any variable must occupy an address, and the 0-1 code in that address space is the value of the variable. Different data types occupy varying sizes of space, but they all have to have an address, which is the basis for hardware access, and the name is just a handy way to remember the address for a programmer. However, different variables are 0-1 code in the machine, so we can't simply check the bits of a value to determine its type.

For example, define the following:

int A;

float B;

Double C;

Long double D;

(assuming that they account for 4,8,8,10, and are stored continuously in an address space and the starting address is 100, we can get the following memory distribution )

A variable consists of 0-1 code in a 4-byte memory space that ends at address 100 to 103. The b variable consists of 0-1 code in 8 bytes of memory space, starting at address 104 to 112. In the machine, the memory is a continuous 0-1 code, the machine does not know that 100~103 is an integral type and 104~111 is a float type, all these types are the compiler tells. When we use a, the compiler knows to take 4 bytes back from the address of a and then interpret it as an int, because it is defined as the int type. So (float) A, the value is first taken out by the int type, and then the value is converted to float according to the rules of int to float. So forcing a type conversion is to take the value of the variable out of the type of a variable and then force the conversion according to the ***to*** rule. If it is a (type name) constant, it is cast by the constant to type rule.

The pointer is also a variable, it occupies a 4-byte address space itself (because the program's addressing space is 2^32, that is, 4GB, so the pointer in 4 bytes is already able to point to any program can address the space, so the size of the pointer is 4 bytes), his value is the address of another thing, This can be a normal variable, a struct, a function, and so on. Since the size of the pointer is 4 bytes, we can cast the pointer to type int or other types. Similarly, we can convert any one constant to an int type and then assign to the pointer. All pointers occupy 4 bytes of space, they just declare different types, their values are addresses to something, they have no essential difference to the machine, they can be forced type conversion.
The coercion type conversion of a pointer to pointer refers to the type of content that the pointer refers to from the original type to the following type.

int a = 1;

int *p = &a;

float *P1 = (float*) p;

The values of P and P1 are &a, but *p is interpreted by the values in the &a address according to the int variable, and *p1 is the value in the &a address according to the The float type variable is interpreted.

Because of the need for this flexible coercion of type conversions between pointers and for simplifying code,ANSI C introduces null pointers, void*. void pointer is also known as a universal pointer, in many of the current programs, when the parameters are not deterministic with the universal pointer instead, this class of pointers in the thread \ Process functions are particularly common.

ANSI c specifies that avoid pointer can be copied to any other pointer of any type, and that any other pointer of any type can be copied to a void pointer, and replication between them does not require coercion of type conversions. Of course any address can also be copied to the void type pointer. We often see in network programming that statements such as accept (socket, struct sockaddr *) &saddr_c, &lenth) need to add code before &saddr_c ( struct sockaddr *) is because ANSI C has not yet proposed the concept of void* when this function is designed . All addresses are identified by a struct sockaddr type, and the second parameter of the function is also a pointer to the struct SOCKADDR type, where the type conversion is enforced.

Of course, in some compilers, different types of pointers can also be assigned directly, but generally a warning of type mismatch is given. Requiring the programmer to display a pointer coercion type conversion can alert the programmer to the use of pointers, with some benefit for explicit program purposes.

1. Pointer type coercion:

int m;

int *pm = &m;

Char *CP = (char *) &m;

PM points to an integral type, CP points to the first byte of the integer number

2. Forced conversion between structures

struct STR1 A;

struct STR2 b;

A= (struct str1) b; This is wrong

a=* (struct str1*) &b); This is correct

3. Explanation of a program

int main (void)


int a[4] = {1, 2, 3, 4};

int *ptr1= (int *) (&a+1);

int *ptr2= (int *) ((int) a+1);

int *c = * (A + 1);

printf ("%x,%x,%x\n", Ptr1[-1], *ptr2,*c);

return 0;


Outputs are 4 and 2000000,2, respectively

The expression &a+1 is a pointer addition operation, not an ordinary numeric addition.

VS2008, where a = 0x001bfc18
(&a + 1) = 0x001bfc28
and a+1 = 0x001bfc1c

The value of &a + 1 depends on the type of a if a declares int A;
Then &a + 1 = 0xffff5704 = a + 1
if int a (Arrylen);
Then &a + 1 = 0xffff5700 + 4 * Arrylen <> A + 1

A represents the starting address of the array, (int) A means that the address of a is converted to an integer number, (int) A + 1 represents a normal numeric addition operation, (int *) ((int) A + 1) that translates (int) A + 1 to the address of an integer pointer. The address points to the first byte of array a (0) (counted from 0), because it is of type int and therefore requires four-byte interpretation, so the result is the value of the second three bytes of a (0) and the first byte of a (1), which is affected by the size end.

* (A + 1) at this point A is already a constant pointer, and the expression calculates the address of the 2nd element following the element a points to, and then references it to get the corresponding value. This expression is equivalent to an

int last = a[1]

Paste the following snippet of code:

#include <stdio.h>typedef struct stu1{    char chs[5];}; typedef struct stu2{    char chs[4];    int  n;}; int main (int argc, char const *argv[]) {    struct stu1 s1;    struct STU2 s2;    S1.chs[0] = ' a ';    S1.chs[1] = ' B ';    S1.CHS[2] = ' C ';    S1.CHS[3] = ' d ';    S1.CHS[4] = ' e ';    S2 = * (struct STU2 *) &s1);    printf ("%c\n", S1.chs[3]);    printf ("%d\n", S2.N);    return 0;}

Result output:

1 D2 101

All types are the same as the underlying nature of the system is the memory of the 0 1 composition. The basic coercion type conversion is to intercept the higher part bits. When assigning a value of type int to a char variable, it retains only its minimum 8 bits, and the high part discards

Looking at a piece of code:

1 #include <stdio.h> 2  3  4 typedef struct stu1{5     int m; 6     int n; 7}; 8 typedef struct stu2{9     Char c1;10     char c2;11};12 int main (int argc, char const *argv[]) {     struct stu1 s1;14     struct STU2 s2;15     s 1.M = 815;  One 0010111116     S1.N =;     s2 = * (struct STU2 *) &s1);     printf ("%d,%d, \ n", S2.c1, S2.C2);     int a = 559;//10 0010111120
   char C = (char) a;21     printf ("%d\n", c);     s1.m = 559;23     s2 = * (struct STU2 *) &s1);     printf ("% D,%d, \ n ", S2.c1, S2.C2);     return 0;26}

Output Result:

47, 3, 4747, 2,

The size of the STU1 struct in the code above is 4 bytes, while the size of the stu2 struct is 2 bytes, so when STU1 is converted to STU2, only the first 2 bytes are reserved. S1.M is an int type s2.ch1 is a char type, and the value of the int type is assigned to a char variable, only its lowest 8 bits are retained, and the high part discards

We can see that the post (low) eight bits of 815 and 559 (one byte) are the same 47. The remaining byte (high eight bits of s1.m) is different from the high eight bits assigned to S2.CH2 815 and 559, so the output is not the same.

The coercion between the C-language pointers is the coercion between the content that the pointer refers to. A byte-by-byte conversion between two bytes. The extra part is intercepted.

C Language pointer type casting

Related Article

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.