An example of C consortium union

  • 2020-05-30 20:50:28
  • OfStack

An example of union in C

1. Definition:


union(int i, short s, char c) un; 
un.i = 3; 
printf( " i=%d " ,un.i); 
printf( " length = %d\n " ,sizeof(un);//==4 , has the largest variable to determine 

2. Equivalent to List T in java

3. Data exchange


void swap(int *p , int *q){ 
int temp = *p; 
*p = *q; 
*q = temp; 
}

4. Print the address


int i = 2; 
printf( " %#x " ,&i);

5. Pointer prints array values


int arr[] = {1,2,3,4}; 
int *p = &arr; 
prinft( " %d\n " , *(p+0)); 
prinft( " %d\n " , *(p+1));

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: