C language structure array and sample code _c language

Source: Internet
Author: User

An array of structures is a structure in which each element of an exponential group is a structural body. In practical applications, structure arrays are often used to denote a group with the same data structure, such as a class student, a workshop worker, etc.

To define the structure body array and define the structure variables in a similar way, see the following example:

struct stu{
  char *name;//name
  int num;//School number
  int age;//ages
  char Group;//group 
  float score;//score
}CL ASS[5];

It means that there are 5 students in one class.

An array of structs can also be initialized when defined, for example:

struct stu{
  char *name;//name
  int num;//School number
  int age;//ages
  char Group;//group 
  float score;//score 
   
    }class[5] = {
  "Li ping", 5,, ' C ', 145.0},
  {"Zhang ping", 4,, ' A ', 130.5},
  {"He fang", 1, ' a ', 1 48.5},
  {"Cheng Ling", 2, ' F ', 139.0},
  {"Wang Ming", 3, E, ' B ', 144.5}
};
   

When all the elements in an array are assigned, the length of the array is not given, for example:

struct stu{
  char *name;//name
  int num;//School number
  int age;//ages
  char Group;//group 
  float score;//score
}CL Ass[] = {
  "Li ping", 5,, ' C ', 145.0},
  {"Zhang ping", 4,, ' A ', 130.5},
  {"He fang", 1, ' a ', 148.5},
   {"Cheng Ling", 2, ' F ', 139.0},
  {"Wang Ming", 3,, ' B ', 144.5}
};

The use of structure arrays is also simple, for example, to get the results of Wang Ming:

Class[4].score;

To modify Li Ping's Study group:

Class[0].group = ' B ';

The example calculates the total score of the class, the average score, and the number of people below 140 points.

#include <stdio.h>
struct{
  char *name;//name
  int num;//School number
  int age;//ages
  char Group;//Group
  float score//Results
} Class[] = {
  "Li ping", 5,, ' C ', 145.0},
  {"Zhang ping", 4,, ' A ', 130.5},
  {"He fang", 1, ' A ', 148.5} ,
  {"Cheng Ling", 2, ' F ', 139.0},
  {"Wang Ming", 3,, ' B ', 144.5}
};
int main () {
  int i, num_140 = 0;
  float average, sum = 0;
  For (i=0 i<5; i++) {
    sum = Class[i].score;
    if (Class[i].score < 140) num_140++;
  }
  printf ("sum=%.2f\naverage=%.2f\nnum_140=%d\n", Sum, SUM/5, num_140);
  return 0;
}

Run Result:

sum=707.50
average=141.50
num_140=2

The above is the C language structure array of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.