Basic C language structure exercises and C language structure exercises

Source: Internet
Author: User
Tags sorted by name

Basic C language structure exercises and C language structure exercises

1. Set the following struct and schema variable definitions:

struct student{    int id;    char *name;    int math;};struct student studl;

Which of the following statements about the assignment of struct type variables is true? ()

A. studl. id = 1001; studl. name = "Tom ";

B. id = 1001; * studl. name = "Tom ";

C. studl. id = 1001; studl. * name = "Tom ";

D. studl. id = 1001L; * studl. name = "Tom ";

2. pointer variables pointing to struct objects can point both to struct variables and to (members in the structure ). There is only one type of struct in 2.1. Is this statement correct?

Error 3: When A struct variable is defined, the memory allocated to it by the system is ()

A. Sum of memory required by each member // including memory completion

B. Amount of memory required for the first member in the Structure

C. The memory size required by Members

D. memory size required by the last member in the structure 4. In the structure, the default access permission of the member is (public. 5. If the default alignment is selected for a 32-bit cpu, the following struct is defined:

Struct {

Unsigned a: 19;

Unsigned B: 11; // four bytes

Unsigned c: 4; // four bytes

Unsigned d: 29; // four bytes

Char index; // four bytes

};

Then the value of sizeof (struct A) is (C)

A.9

B. 12

C. 16

D. 20 6. Functions of the following code include defining an array x, indicating a struct, and initializing the variable t to make the value of a member of t 50, the value of member B is the first address of array x.

Enter the appropriate content in the blank space (in the box.

int x[5]={1,2,3,4,5};struct { int a; int *b;}t{ (50),(&x) };

7. When developing C code, we often see the following types of struct definitions:

typedef struct list_t{struct list_t *next;//4struct list_t *prev;//4char data[0];//0}list_t;

In a 32-bit system, what is the value of sizeof (list_t? (B)

A. 4 byte

B .8 byte

C. 5 byte

D. 9 byte

-8. Define a struct variable (including year, month, and day), and calculate the day of the current year as the day? (Pay attention to the leap year issue), you need to write a function days to implement the above calculation. The main function passes the year, month, and day to the days function. After calculation, the day is passed back to the main function output.

# Include
 
  
Int days (int year, char month, char day); struct data {int year; char month; char day ;}d ={ 2000, 3, 1 }; int mon [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 31}; void main (void) {int day = days (d. year, d. month, d. day); printf ("% d \ n", day);} int days (int year, char month, char day) {int I = 0, days = 0; if (year % 100! = 0 & year % 4 = 0) | (year % 400 = 0) mon [1] + = 1; for (I = 0; I
  
   
9. Suppose there is a struct about Employee information: struct Employee {long eID; char eName [10]; struct Employee * pNext ;}; eID indicates Employee ID, compile a function: struct Employee * eIDSequence (int eID, char * sName)
   
Write code snippets here

10. Design a struct type named student. A member of this type has a character type variable named sex and an int type named id, there is a variable named name character array. Please create an sruc student type array in the main function, the array length is 10, then, a user-defined function is designed to input the struct array, and a user-defined function is designed to find the element with the largest id value in the struct array and display the values of each member.

#include 
    
     typedef struct student{ char sex;int id;char name[10];}S;void strin(S *p);void idmax(S *p);void main (void){    S s[10];    int i = 0;    for(i=0; i<10; i++)        strin(&s[i]);    idmax(s);    return;}inline void strin(S *p){    scanf("%d %d %s", &(p->sex), &(p->id), &(p->name));    return;}void idmax(S *p){    int num = 0, i = 0;    for(i=1; i<10; i++)    {        if((p+i)->id > (p+num)->id)        {            num = i;        }    }    printf("%d\n%d\n%s\n", (p+num)->sex, (p+num)->id, (p+num)->name);    return;}
    

11. Calculate the linear distance between two points by programming.

Requirement: the coordinate points adopt the struct type. The screen obtains the input two points and the output distance (two digits after the decimal point ).

Write code snippets here

12. Title:

Phone Book Management

Description:

Input the names and phone numbers of the five users in an array of struct types, sort them alphabetically by name (keep their names in their original locations), and output their names and phone numbers. The known struct types are as follows: struct user {char name [20]; char num [10] ;}; input description:

Enter the name string and phone number string. Output description:

The name string and phone number string sorted by name are output. Style input:

Aa

12345

Dd

23456

Cc

34567

Bb

21456

Ee

12456 style output:

Aa

12345

Bb

21456

Cc

34567

Dd

23456

Ee

12456

Related Article

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.