Structure learning (2)

When we mentioned structures and arrays last time, the combination of pointers is more useful. Today's learning content is the structure array.

In fact, the defined structure variables are like arrays.

struct student
{
    
    
	int num;
	char *name;
	char sex;
	float score;
}boy1,boy2={
    
    102,"jane",'M',98.5};

Just like this code, the initialization of boy2.

Note The difference between a
structure array and the numeric arrays introduced before is that each array element is a structure type data, and they all include each member (component) item.

For example, a structure array is used to complete the address book function.

#include<stdio.h>
#include<stdlib.h>
#define num 3
struct persons 
{
    
    
	char name[20];
	char phonenumbers[20];
}h[num];
int main()
{
    
    
	int i;

	for(i=0;i<num;i++)
	{
    
    
		printf("input name\n");
		gets(h[i].name);
		printf("input phonenumbers\n");
		gets(h[i].phonenumbers);
	}
	printf("\tname\t\t\t\t\tphone\n\n");

	for(i=0;i<num;i++)
	{
    
    
		printf("%20s\t\t\t%20s\n",h[i].name,h[i].phonenumbers);
	}
}

First saw the structure array function.

It can be initialized in the following way.
struct student str[]{ {…},{…},{…}};

Next, we will practice the structure array. The content does not seem to be much, but the programming questions are not small.

Guess you like

Origin blog.csdn.net/yooppa/article/details/112547455