SoFunction
Updated on 2025-04-06

Android NDK development (C language--consortium and enumeration)

1. Consortium

A communicator is a special data type that allows you to store different data types in the same memory location. You can define a common body with multiple members, but at any time there can only be one member with a value. Communities provide an efficient way to use the same memory location.

1.1 Defining a common

In order to define a common body, you must useunion The statement has a similar way to the definition structure.union A statement defines a new data type with multiple members.

The format of the union statement is as follows:

union [union tag]
{
   member definition;
   member definition;
   ...
   member definition;
} [one or more union variables];


union tagis optional, eachmember definition is a standard variable definition, e.g. int i; orfloat f;Or other valid variable definitions. At the end of the common definition, before the last semicolon, you can specify one or more common variables, which is optional. The following defines a common type named Data, with three members i, f, and str:

union Data
{
   int i;
   float f;
   char  str[20];
} data;


Now,Data A variable of type can store an integer, a floating point number, or a string. This means that a variable (the same memory location) can store multiple types of data. You can use any built-in or user-defined data types within a shared body as needed.

1.2 The memory occupied by the communicator should be sufficient to store the largest member of the communicator.

For example, in the above example, Data will take up 20 bytes of memory space because the string takes up the largest space among each member. The following example will show the total memory size occupied by the above common body:

union Data
{
   int i;
   float f;
   char  str[20];
};
 
void main( )
{
   union Data data;        
 
   printf( "Memory size occupied by data : %d\n", sizeof(data));
 
   system("pause");
}


Results output:

Memory size occupied by data : 20

1.3 Only one variable exists at any time, and the last assignment is valid.

union  MyValue {

    int x;
    int y;
    double z;

};



void main() {

    union MyValue d1;

     = 90;

     = 100; //The last assignment is valid
    // = 23.8;

    printf("%d , %d, %lf\n", , , );

     = 23.8;
    printf("%d, %d, %lf\n", , , );

    system("pause");

}

Results output:

100 , 100, -92559592117433135502616407313071917486139351398276445610442752.000000
-858993459, -858993459, 23.800000

1.4 Union in JNI header file

typedef union jvalue {
    jboolean    z;
    jbyte       b;
    jchar       c;
    jshort      s;
    jint        i;
    jlong       j;
    jfloat      f;
    jdouble     d;
    jobject     l;
} jvalue;


2. Enumeration

Enumeration (list all cases), limit the value range of the value to ensure the security of the value.

enum Day
{
    Monday,//The default is 0, and the value of subsequent enumeration members is added to the previous member by 1.    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};


void main() {
    //The value of the enum must be the value in brackets    enum Day d = Monday;
    printf("%#x,%d\n", &d, d);

     d = Wednesday;
    printf("%#x,%d\n", &d, d);

    getchar();
}

Results output:

0xdaaff5e4,0
0xdaaff5e4,2

  • (1) An enumeration type is a collection, and the elements (enumeration members) in the collection are some named integer constants, separated by commas.
  • (2) DAY is an identifier, which can be regarded as the name of this collection, and is an optional item, that is, an optional item.
  • (3) The default value of the first enumeration member is 0 of an integer, and the value of the subsequent enumeration member is added to the previous member.
  • (4) You can manually set the values ​​of enumerated members to customize integers within a certain range.
  • (5) Enumeration type is a preprocessing instruction#definealternative.
  • (6) The type definition ends with a semicolon;

Comprehensive examples:

enum Season
{
    spring, summer = 100, fall = 96, winter
};

typedef enum
{
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Weekday;

void main()
{
    /* Season */
    printf("%d \n", spring); // 0
    printf("%d, %c \n", summer, summer); // 100, d
    printf("%d \n", fall + winter); // 193
    enum Season mySeason = winter;
    if (winter == mySeason)
        printf("mySeason is winter \n"); // mySeason is winter

    int x = 100;
    if (x == summer)
        printf("x is equal to summer\n"); // x is equal to summer

    printf("%d bytes\n", sizeof(spring)); // 4 bytes

                                          /* Weekday */
    printf("sizeof Weekday is: %d \n", sizeof(Weekday)); //sizeof Weekday is: 4

    Weekday today = Saturday;
    Weekday tomorrow;
    if (today == Monday)
        tomorrow = Tuesday;
    else
        tomorrow = (Weekday)(today + 1); //remember to convert from int to Weekday


    getchar();
}

Results output:

0
100, d
193
mySeason is winter
x is equal to summer
4 bytes
sizeof Weekday is: 4

This is the article about Android NDK development (C language-consortium and enumeration). For more related C language-consortium and enumeration content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!