zoukankan      html  css  js  c++  java
  • 结构

    定义结构:定义结构,必须使用 struct 语句。struct 语句定义了一个包含多个成员的新的数据类型。访问结构体里面的成员的时候我们用 . 这个成员访问符。

    1 struct type_name   //关键字struct
    2  {
    3     type1    name1;  //定义类型,如 int ID;
    4     type2    name2;
    5     type3    name3;
    6 .
    7 .
    8 } object_names;   //这是一个可选的结构变量

    举个栗子:

     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 struct books
     6 {
     7     int ID;
     8     char name[100];
     9     char code[100];
    10     char auther[100];
    11 } bookthree ;    //定义变量
    12 
    13 int main()
    14 {
    15     books bookone;    //注意这里是变量,跟bookthree一样的效果
    16     books booktwo;      //定义结构体类型books的变量booktwo
    17     
    18     bookone.ID = 24615;
    19     strcpy(bookone.name, "雏田");
    20     strcpy(bookone.code, "CX520");
    21     strcpy(bookone.auther, "火影忍者");
    22 
    23     bookone.ID = 24520;
    24     strcpy(booktwo.name, "");
    25     strcpy(booktwo.code, "CD520");
    26     strcpy(booktwo.auther, "火影忍者");
    27     
    28     strcpy(bookthree.name, "止水");
    29     strcpy(bookthree.auther, "火影忍者");
    30 
    31     cout << "第一本书的名字:" << bookone.name << endl;
    32     cout << "第一本书的作者:" << bookone.auther << endl <<endl;
    33     cout << "第二本书的名字:" << booktwo.name << endl;
    34     cout << "第二本书的作者:" << booktwo.auther << endl <<endl;
    35     cout << "第三本书的名字:" << bookthree.name << endl;   
    36     cout << "第三本书的作者:" << bookthree.auther << endl ;
    37     return 0;
    38 }

    结果为:

    第一本书的名字:雏田
    第一本书的作者:火影忍者
    
    第二本书的名字:鼬
    第二本书的作者:火影忍者
    
    第三本书的名字:止水
    第三本书的作者:火影忍者

     结构作为参数:可以把结构作为函数参数,传参方式与其他类型的变量或指针类似。举个栗子:

     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 struct books   //结构体books
     6 {
     7     int Id;
     8     char Name[100];
     9     int Date;
    10     char Auther[100];
    11 } book;
    12 
    13 void temp(struct books book)   //结构作为参数传递,跟变量一样到用法
    14 {
    15     cout << "书名:" << book.Name << endl;
    16     cout << "作者:" << book.Auther << endl;
    17     cout << "日期:" << book.Date << endl;
    18 }
    19 
    20 int main()
    21 {
    22     book.Id = 24520;       //这个地方进行结构的赋值
    23     strncpy(book.Name, "伊塔奇", sizeof("伊塔奇") - 1);
    24     strncpy(book.Auther, "火影忍者11", 10);
    25     book.Date = 20190528;
    26     temp(book);
    27     return 0;
    28 }

     结果为:

    书名:伊塔奇
    作者:火影忍者11
    日期:20190528

     指向结构的指针:定义指向结构的指针,方式与定义指向其他类型变量的指针相似,如: struct Books *book1; 结构变量的地址: book1 = &Book1; 为了使用指向该结构的指针访问结构的成员,您必须使用 -> 运算符,如: book1->ID 举个栗子:

     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 struct books   //结构体books
     6 {
     7     int Id;
     8     char Name[100];
     9     int Date;
    10     char Auther[100];
    11 } book;
    12 
    13 void temp(struct books *book)   //结构变量的地址作为参数传递 注意这里的struct可以省略
    14 {
    15     cout << "书名:" << book->Name << endl;  //这里使用->来访问结构成员
    16     cout << "作者:" << book->Auther << endl;
    17     cout << "日期:" << book->Date << endl;
    18 }
    19 
    20 int main()
    21 {
    22     book.Id = 24520;       //这个地方进行结构的赋值
    23     strncpy_s(book.Name, "伊塔奇", sizeof("伊塔奇") - 1);
    24     strncpy_s(book.Auther, "火影忍者11", 10);
    25     book.Date = 20190528;
    26     temp(&book);  //取结构体变量book的地址
    27     return 0;
    28 }

    结果跟上一致。

  • 相关阅读:
    Oracle:SQL语句--对表的操作——修改列的数据类型( 即 修改字段的数据类型)
    Oracle:SQL语句--对表的操作——修改列的数据类型( 即 修改字段的数据类型)
    有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
    有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
    鸡兔同笼:笼子里一共有鸡和兔子35只,一共有94条退, 笼子里一共有鸡和兔子共多少只
    jQuery,使用on代替delegate,live 写法区别
    安卓手机微信页面position: fixed位置错误
    表单提交是ajax提交,PC提交没问题但是手机提交就会一直跳到error,并且也没状态码一直是0
    新版本的jquery checkbox 全选反选代码只能执行一遍,第二次就失败attr与prop区别
    倒计时js代码
  • 原文地址:https://www.cnblogs.com/xiaodangxiansheng/p/10933947.html
Copyright © 2011-2022 走看看