Why can id refer to any type

Just understand a little bit, alloc the returned object is actually a isa address, that is to say, even if the two objects of the actual distribution in the structure of the body size is not the same, but the structure of address is the address of the first variable structure, so simply an object pointer, pointing to a 8 bytes of memory address, That’s not going to be a problem in terms of size and the reason why we’re asking this is because it’s wrong in C, because the type that the pointer points to is not the same size, so when you read the data from the pointer, it’s going to read by size

int *p1;
double x=0;
double *p2 = &x ;
p1=p2;
Copy the code

#### Preliminary exploration objective: To test the internal location of structures in C

typedef struct test{
    int a;
    int b;
    int c;
    int d;
}test;

 test* t2 = (test*)malloc(sizeof(test));
 t2->a=1000;
 int* t3 = t2;
Copy the code

Print out the value that T3 points to, which is actually 1000, to verify that the address stored in the structure is actually the address of the first variable:

double* t3 = t2;
Copy the code

This is not an error, but the value is read as 64 bits of double, and it does not print the expected value #### The first variable in this structure is Class ISA so you assign any type to it and you’re just giving it the address of ISA, so you can actually see that this works as well

UITableView *a=[[UIButton alloc] init];
Copy the code

Then we can use the advantage of the id type is logically deal with generics, is referred to as dynamic type is because at the time of compilation, the id variable which class, don’t know oneself to know at run time, will the class assigned to the id, it didn’t know I what class, and thus can see is not the so-called casts.