zoukankan      html  css  js  c++  java
  • 结构体

    #include<stdio.h>
    //2020年11月19日16:46:44
    //修改结构体变量成员的值-两种方式
    struct Student
    {
    int age;
    float score;
    char sex;
    };
    int main(void)
    {
    struct Student st = {11,23.3,'F'};//定义的同时赋初值;这是第一种方式
    //但一般不使用,因为不可能为每一块都分配一个名字,太麻烦
    st.score=66.6f;//第一种方式,66.6在C语言中默认是double类型,
    //如果希望它是float类型,需要在末尾加f或者F

    struct Student * pst = &st;//第二种方式,用一个指针变量指向了结构体变量
    //何为指向?即指针变量存储了结构体变量的地址
    pst->age=88;//pst->age等价于(*pst).age等价于st.age
    //这个代码的意思是pst所指向的结构体变量中的age这个成员
    printf("%d,%f ",st.age,st.score);
    return 0;
    }


     1 #include<stdio.h>
     2 #include<string.h>
     3 struct Student
     4 {
     5     char name[100];
     6     int age;
     7     int sid;
     8 };
     9 int main(void)
    10 {
    11     struct Student st={"zhangsan",10,23};
    12     printf("%s,%d,%d
    ",st.name,st.age,st.sid);
    13     //st.name="zhangsan";//error,name代表的首元素地址
    14     strcpy(st.name,"zhangsan");
    15     st.age=10;
    16     st.sid=23;
    17     printf("%s,%d,%d
    ",st.name,st.age,st.sid);
    18     return 0;
    19 }

    注意字符数组的赋值方式哈

  • 相关阅读:
    c# 正则表达式 首字母转大写
    c# WebBrowser获取cookie
    c# 求最小公倍数
    Response.Redirect与Server.Transfer区别-转
    asp 读文件 比较ip
    asp数组的使用
    如何解决#1045
    mysql limit分页查询效率
    Docker 容器管理:rancher
    Docker监控:google/cadvisor
  • 原文地址:https://www.cnblogs.com/ajiaoa/p/14006512.html
Copyright © 2011-2022 走看看