[C Language] Compound Type - 009

Chapter 9 Compound Types (Custom Types)

9.1 Structure

9.1.1 Overview

Array: Describes an ordered set of data of the same type used to process a large number of data operations of the same type.

Sometimes we need to combine different types of data into an organic whole, such as: a student has attributes such as school number/name/gender/age/address. Obviously, it is very complicated to define the above variables separately and the data is not easy to manage.

In C language, another type of structured data, structured body, is given.

Definition and Initialization of 9.1.2 Structural Variables

How structural variables are defined:

  • Declare the structure type before defining the variable name
  • Define variables while declaring types
  • Direct Definition of Structural Type Variables (Untyped Names)

Structure type and structure variable relationship:

  • Structural Type: Specifies a structure type that corresponds to a model, but does not contain specific data, nor does the system allocate actual memory units to it.
  • Structural variables: The system allocates space according to the type of structure (internal member status).
//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

//Define the type first, then the variable (commonly used)
struct stu s1 = { "mike", 18 };


//Define types and variables at the same time
struct stu2
{
	char name[50];
	int age;
}s2 = { "lily", 22 };

struct
{
	char name[50];
	int age;
}s3 = { "yuri", 25 };

9.1.3 Use of Structural Members

#include<stdio.h>
#include<string.h>

//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

int main()
{
	struct stu s1;

	//If it is a normal variable, the structure members are manipulated by a point operator
	strcpy(s1.name, "abc");
	s1.age = 18;
	printf("s1.name = %s, s1.age = %d\n", s1.name, s1.age);

	//If it is a pointer variable, manipulate the structure members by - >.
	strcpy((&s1)->name, "test");
	(&s1)->age = 22;
	printf("(&s1)->name = %s, (&s1)->age = %d\n", (&s1)->name, (&s1)->age);

	return 0;
}

9.1.4 Structural Array

#include <stdio.h>

//Statistical achievement
struct stu
{
	int num;
	char name[20];
	char sex;
	float score;
};

int main()
{
	//Define and initialize an array of structured bodies with five elements
	struct stu boy[5] = {
		{ 101, "Li ping", 'M', 45 },			
		{ 102, "Zhang ping", 'M', 62.5 },
		{ 103, "He fang", 'F', 92.5 },
		{ 104, "Cheng ling", 'F', 87 },
		{ 105, "Wang ming", 'M', 58 }};

	int i = 0;
	int c = 0;
	float ave, s = 0;
	for (i = 0; i < 5; i++)
	{
		s += boy[i].score;	//final scoring
		if (boy[i].score < 60)
		{
			c += 1;		//Statistics of failures
		}
	}

	printf("s=%f\n", s);//Print total score
	ave = s / 5;					//Calculate the average score
	printf("average=%f\ncount=%d\n\n", ave, c); //Print average score and number of failures


	for (i = 0; i < 5; i++)
	{
		printf(" name=%s,  score=%f\n", boy[i].name, boy[i].score);
           // printf(" name=%s,  score=%f\n", (boy+i)->name, (boy+i)->score);

	}

	return 0;
}

9.1.5 sleeve structure

#include <stdio.h>

struct person
{
	char name[20];
	char sex;
};

struct stu
{
	int id;
	struct person info;
};

int main()
{
	struct stu s[2] = { 1, "lily", 'F', 2, "yuri", 'M' };

	int i = 0;
	for (i = 0; i < 2; i++)
	{
		printf("id = %d\tinfo.name=%s\tinfo.sex=%c\n", s[i].id, s[i].info.name, s[i].info.sex);
	}

	return 0;
}

9.1.6 Structure Assignment

#include<stdio.h>
#include<string.h>

//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

int main()
{
	struct stu s1;

	//If it is a normal variable, the structure members are manipulated by a point operator
	strcpy(s1.name, "abc");
	s1.age = 18;
	printf("s1.name = %s, s1.age = %d\n", s1.name, s1.age);

	//Two structural variables of the same type can be assigned to each other.
	//Copy the value of s1 member variable to the memory of s2 member variable
	//s1 and s2 are just the same values as the member variables. They are two variables that are not related.
	struct stu s2 = s1;
//memcpy(&s2, &s1, sizeof(s1));
	printf("s2.name = %s, s2.age = %d\n", s2.name, s2.age);

	return 0;
}

9.1.7 Structure and Pointer

1. Points to variables of ordinary structures

#include<stdio.h>

//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

int main()
{
	struct stu s1 = { "lily", 18 };

	//If it is a pointer variable, manipulate the structure members by - >.
	struct stu *p = &s1;
	printf("p->name = %s, p->age=%d\n", p->name, p->age);
	printf("(*p).name = %s, (*p).age=%d\n",  (*p).name,  (*p).age);

	return 0;
}

2. Reactor Area Structural Volume Variables

#include<stdio.h>
#include <string.h>
#include <stdlib.h>

//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

int main()
{
	struct stu *p = NULL;

	p = (struct stu *)malloc(sizeof(struct  stu));

	//If it is a pointer variable, manipulate the structure members by - >.
	strcpy(p->name, "test");
	p->age = 22;

	printf("p->name = %s, p->age=%d\n", p->name, p->age);
	printf("(*p).name = %s, (*p).age=%d\n", (*p).name,  (*p).age);

	free(p);
	p = NULL;

	return 0;
}

3. First-order pointer of structure sleeve

#include<stdio.h>
#include <string.h>
#include <stdlib.h>

//Definition of Structural Type
struct stu
{
	char *name; //Primary pointer
	int age;
};

int main()
{
	struct stu *p = NULL;

	p = (struct stu *)malloc(sizeof(struct  stu));

	p->name = malloc(strlen("test") + 1);
	strcpy(p->name, "test");
	p->age = 22;

	printf("p->name = %s, p->age=%d\n", p->name, p->age);
	printf("(*p).name = %s, (*p).age=%d\n", (*p).name, (*p).age);

	if (p->name != NULL)
	{
		free(p->name);
		p->name = NULL;
	}

	if (p != NULL)
	{
		free(p);
		p = NULL;
	}

	return 0;
}

9.1.8 Structures as Functional Parameters

1. Ordinary variables of structure as function parameters

#include<stdio.h>
#include <string.h>

//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

//Function parameters are general variables of structure
void set_stu(struct stu tmp)
{
	strcpy(tmp.name, "mike");
	tmp.age = 18;
	printf("tmp.name = %s, tmp.age = %d\n", tmp.name, tmp.age);
}

int main()
{
	struct stu s = { 0 };
	set_stu(s); //pass by value
	printf("s.name = %s, s.age = %d\n", s.name, s.age);

	return 0;
}

2. Structure pointer variables as function parameters

#include<stdio.h>
#include <string.h>

//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

//Function parameter is pointer variable of structure
void set_stu_pro(struct stu *tmp)
{
	strcpy(tmp->name, "mike");
	tmp->age = 18;
}

int main()
{
	struct stu s = { 0 };
	set_stu_pro(&s); //Address delivery
	printf("s.name = %s, s.age = %d\n", s.name, s.age);

	return 0;
}

3. Structural array names as function parameters

#include<stdio.h>

//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

//void set_stu_pro(struct stu tmp[100], int n)
//void set_stu_pro(struct stu tmp[], int n)
void set_stu_pro(struct stu *tmp, int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		sprintf(tmp->name, "name%d%d%d", i, i, i);
		tmp->age = 20 + i;
		tmp++;
	}
}

int main()
{
	struct stu s[3] = { 0 };
	int i = 0;
	int n = sizeof(s) / sizeof(s[0]);
	set_stu_pro(s, n); //Array name passing

	for (i = 0; i < n; i++)
	{
		printf("%s, %d\n", s[i].name, s[i].age);
	}

	return 0;
}

4.const Modified Pointer Parametric Variables of Structures

//Definition of Structural Type
struct stu
{
	char name[50];
	int age;
};

void fun1(struct stu * const p)
{
	//p = NULL; //err
	p->age = 10; //ok
}

//void fun2(struct stu const*  p)
void fun2(const struct stu *  p)
{
	p = NULL; //ok
	//p->age = 10; //err
}

void fun3(const struct stu * const p)
{
	//p = NULL; //err
	//p->age = 10; //err
}

9.2 Common Community (Commonwealth)

  • Joint union is a type of data that can store different types of data in the same storage space.
  • The length of memory occupied by a consortium is equal to the length of its longest member, which is also called a common body.
  • The same memory segment can be used to store several different types of members, but only one function at each instant.
  • The member that plays a role in the common variable is the last member stored, and the value of the original member will be overwritten when a new member is stored.
  • The address of the common variable and the address of its members are the same address.
#include <stdio.h>

//Commons are also called consortia. 
union Test
{
	unsigned char a;
	unsigned int b;
	unsigned short c;
};

int main()
{
	//Define Common Volume Variables
	union Test tmp;

	//1. The first address of all members is the same.
	printf("%p, %p, %p\n", &(tmp.a), &(tmp.b), &(tmp.c));

	//2. The size of the shared body as the largest member type
	printf("%lu\n", sizeof(union Test));

	//3. A member assignment affects another member
	//High on the left and low on the right
	//Lower address, higher address
	tmp.b = 0x44332211;

	printf("%x\n", tmp.a); //11
	printf("%x\n", tmp.c); //2211

	tmp.a = 0x00;
	printf("short: %x\n", tmp.c); //2200
	printf("int: %x\n", tmp.b); //44332200

	return 0;
}

9.3 Enumerations

Enumeration: List the values of variables one by one. The values of variables are limited to the range of the values listed.

Enumeration type definition:

enum enumeration
{
	Enumeration table
};
  • All available values, also known as enumeration elements, should be listed in the enumeration value table.
  • Enumeration values are constants and cannot be assigned to programs with assignment statements.
  • The enumeration element itself is defined by the system as a numeric value representing the ordinal number from 0 to 0, 1, 2, and so on.
#include <stdio.h>

enum weekday
{
	sun = 2, mon, tue, wed, thu, fri, sat
} ;

enum bool
{
	flase, true
};

int main()
{
	enum weekday a, b, c;
	a = sun;
	b = mon;
	c = tue;
	printf("%d,%d,%d\n", a, b, c);

	enum bool flag;
	flag = true;

	if (flag == 1)
	{
		printf("flag To be true\n");
	}
	return 0;
}

9.4 typedef

Typeedef is the key word of C language. Its function is to define a new name for a data type (basic type or custom data type), and it cannot create a new type.

  • Unlike # define, typedef is limited to data types, not expressions or specific values.
  • # define occurs in preprocessing, typedef occurs in compilation
#include <stdio.h>

typedef int INT;
typedef char BYTE;
typedef BYTE T_BYTE;
typedef unsigned char UBYTE;

typedef struct type
{
	UBYTE a;
	INT b;
	T_BYTE c;
}TYPE, *PTYPE;

int main()
{
	TYPE t;
	t.a = 254;
	t.b = 10;
	t.c = 'c';

	PTYPE p = &t;
	printf("%u, %d, %c\n", p->a, p->b, p->c);

	return 0;
}

Keywords: C

Added by Mark1inLA on Fri, 23 Aug 2019 12:53:30 +0300