C Language Learning: Strings, Structures, Commons

Character string

In C, strings are actually one-dimensional character arrays terminated with the null character'\0'.Therefore, a null-terminated string contains the characters that make up the string.Declare and initialize create a string:

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

In fact, you do not need to place the null character at the end of a string constant.The C compiler automatically places'\0'at the end of the string when it initializes the array.

Functions with a large number of operation strings in C (more than those listed in the following table):

Sequence Number Function & purpose
1 strcpy(s1, s2);
Copy string s2 to string s1.
2 strcat(s1, s2);
Connection string s2 to the end of string s1.
3 strlen(s1);
Returns the length of the string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and S2 are the same; less than 0 if s1 < s2; greater than 0 if s1 > s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of the character ch in the string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.

structural morphology

Arrays in C allow you to define variables that can store items of the same type, and the structure is another user-defined data type available in C programming that allows you to store different types of data items.

Declare Structures and Structural Variables

The struct statement defines a new data type that contains multiple members in the following format:

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

In general, structTag (struct tag (class name), typei attrVarName; (member attribute definition), objectvarNamei (instantiated variable name (object)) are at least two parts.

Examples of declarations are as follows:

//This declaration declares a structure with three members (integer a, character b, and double c) and no label, and declares the structure variable s1.
    int a;
    char b;
    double c;
} s1;
//This declaration declares a structure with three members and a label named SIMPLE, without declaring a structure variable
struct SIMPLE
    int a;
    char b;
    double c;
//Structures labeled with SIMPLE, with structure variables t1, t2, t3 declared
struct SIMPLE t1, t2[20], *t3;

Among the two declarations mentioned above, the structural variable and s1 declared by SIMPLE are two completely different types.

We can also use the typedef keyword to create a new type, where Simpler is the name of the new type created, and then it can be used to create instance variables of this type.Examples are as follows:

typedef struct
    int a;
    char b;
    double c; 
} Simple2;
//Simple2 can now be used as a type to declare new structure variables
Simple2 u1, u2[20], *u3;

Members of a structure can contain other structures or pointers to their own structure types, such as implementing data structures such as chains and trees.

1. The Declaration contains other structures

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

2. The Declaration contains a pointer to its own type

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

3. If two structures are contained in each other, an incomplete declaration of one of them is required.

struct B;    //Incomplete declaration of structure B
//Structure A contains a pointer to structure B
struct A
    struct B *partner;
    //other members;
//Structure B contains a pointer to structure A, which is declared after A is declared
struct B
    struct A *partner;
    //other members;

Initialization of Structural Variables

As with other types of variables, struct variables can be defined with initial values specified.

Access structure members

Use member access operator (.).The member access operator is an English period between the name of the structural variable and the member of the structure we want to access.

Structure as a function parameter

Pointer to structure

Defines a pointer to a structure similar to a pointer to another type of variable in the following syntax:

struct structTag *structPointer;
structPointer = &structVarName

When accessing members of a structure using a pointer to the structure, you must use the -> operator.

//book is a structure pointer to Books type of structure variable book1 of Books type (pointer type to variable needs to be the same type as variable type)
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;        /* Declare Book1 of type Books */
    /* Book1 Member Initialization */
    //String member variables are initialized using strcpy
    strcpy( Book1.title, "C Programming");
    strcpy( Book1.author, "Nuha Ali"); 
    strcpy( Book1.subject, "C Programming Tutorial");
    Book1.book_id = 6495407;

    printBook( &Book1 );

Common body

Common body is a special data type that allows different data types to be stored in the same memory location.You can define a common body with multiple members, but only one of them can have a value at any time.

Define Commons

Use the union statement in a manner similar to defining the structure.The union statement defines a new data type with multiple members.The union statement is formatted as follows:

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

unionTag, unionVariables are optional.The memory occupied by the common body should be sufficient to store the largest member in the common body.

Access Common Body Members

To access members of a common body, we use the member access operator (.).Use the union keyword to define variables of a common entity type in the same way as struct:

union unionTag unionVar

All members can output well provided that only one member is used at the same time, and subsequently initialized members of the common body override those previously initialized.

160 original articles published. 90% praised. 50,000 visits+
Private letter follow

Keywords: Programming less Attribute

Added by fluvly on Sat, 18 Jan 2020 04:25:49 +0200