C language learning: strings, structures, unions

String

In the C language, using the string actually  null  character '\ 0' one-dimensional array of characters terminated. Therefore, a null-terminated string that contains the character string. Declare and initialize created a string:

char charArrayName[capacity] = {charElement1,charElement2...}

In fact, you do not need the  null  character at the end of the string constants. When the C compiler array initialization, automatically '\ 0' on the end of the string.

C function to operate a large number of strings (not just listed in the table below):

No. Function & Objective
1 strcpy (s1, s2);
copy the string to the string s2 s1.
2 strcat (s1, s2);
connection string s1 s2 to the end of the string.
3 strlen (s1);
returns the length of the string of s1.
4 strcmp (s1, s2);
If s1 and s2 are the same, then return 0; if s1 <s2 is less than 0 is returned; if s1> s2 is greater than 0 is returned.
5 the strchr (s1, ch);
Returns a pointer to the location of the first character string s1 first occurring ch.
6 Strstr (s1, s2);
Returns a pointer to the location of the string s1 s2 of the first occurrence of the string.

Structure

C in an array may be defined to allow variables of the same type stored data items , the structure is available in the C programming data type to another user-defined, which allows the storage of data items of different types.

Declaration of structure and structure variable

struct statement defines a new data type comprising a plurality of members, the format struct statement is:

struct structTag { 
    type1 attrVarName;
    type2 attrVarName;
    ...
    typeN attrVarName;
} structVarName1,structVarName2,...,structVarNameN;

In general, structTag (tag structure (class name)), typei attrVarName; (attribute definition members), objectvarNamei (instantiated variable name (object)) that appeared to be at least 3 part 2.

To declare the following example:

//此声明声明了拥有3个成员(整型的a,字符型的b和双精度的c)并且没有标明其标签的结构体,声明了结构体变量s1。
struct 
{
    int a;
    char b;
    double c;
} s1;
 
//此声明声明了拥有3个成员并且标签命名为SIMPLE的结构体,没有声明结构体变量
struct SIMPLE
{
    int a;
    char b;
    double c;
};
//用SIMPLE标签的结构体,另外声明了结构体变量t1、t2、t3
struct SIMPLE t1, t2[20], *t3;

 Both of the above statement, and s1 SIMPLE structure variable declarations are two completely different types.

We can also create a new type with typedef keyword , which, Simpler creation of a new type name, then you can use it to create this type of instance variables. Examples are as follows:

typedef struct
{
    int a;
    char b;
    double c; 
} Simple2;
//现在可以用Simple2作为类型声明新的结构体变量
Simple2 u1, u2[20], *u3;

 Members of the structure may comprise other structures may also comprise a structure pointing to its own types of indicators , such as trees, and a linked list implementation data structure.

1. The declaration includes other structures

struct COMPLEX
{
    char string[100];
    struct SIMPLE a;
};
 

2. The declaration contains a pointer pointing to their own type of 

struct NODE
{
    char string[100];
    struct NODE *next_node;
};

 3. The structure comprises two each other, the need for a structure body wherein Incomplete Statement

struct B;    //对结构体B进行不完整声明
 
//结构体A中包含指向结构体B的指针
struct A
{
    struct B *partner;
    //other members;
};
 
//结构体B中包含指向结构体A的指针,在A声明完后,B也随之进行声明
struct B
{
    struct A *partner;
    //other members;
};

Initialization of the structure variable

And other types of variables, the initial value can be specified in the definition of structure variables.

Access structure members

Using the member access operator (.) . Access operator is a member of periods between architecture and structural members of our variable names to be accessed;

Structure as a function parameter

Pointer to the structure

The pointer points to define the structure of the embodiment defined pointer point to other similar types of variables, the following syntax:

//声明
struct structTag *structPointer;
//初始化
structPointer = &structVarName

Using the pointer to the structure of the pointer when accessing the members of the structure must make -> operator.

//book为指向 Books类型的结构体变量book1 的 Books类型的结构体指针(指向变量的指针类型需和变量的类型相同)
void printBook( struct Books *book )
{
   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);
}
int main(){
    struct Books Book1;        /* 声明 Book1,类型为 Books */
    /* Book1 成员初始化 */
    //字符串成员变量使用strcpy初始化
    strcpy( Book1.title, "C Programming");
    strcpy( Book1.author, "Nuha Ali"); 
    strcpy( Book1.subject, "C Programming Tutorial");
    Book1.book_id = 6495407;

    printBook( &Book1 );
}

Union

The union is a special data type, allowing at the same memory location to store different types of data. Can define a common body with a plurality of members, but any time only one of the members with the value.

Defined union

Use the  union  statement, in a similar manner to define the structure. union statement defines a new data type, with multiple members. union statement format is as follows:

union unionTag
{
   member definition;
   member definition;
   ...
   member definition;
} one or more unionVariables;

unionTag  , unionVariables are optional. It should be sufficient to store the largest member of the union union memory occupied.

Access union member

Access to the common body member, we use the member access operator (.). Use  union  keyword to define a common type of variable, in the same manner and struct:

union unionTag unionVar

All members can well premise output is used only one time a member of the initialization before the initialization of union members will be covered.

Published 161 original articles · won praise 90 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_42415326/article/details/104027378