sizeof,strlen,结构体

一、sizeof
1.作用:
用来计算一个变量或一个常量,一种数据类型所占用的内存字节数;
2.基本类型
sizeof(变量或常量)
sizeof(数据类型)
不能是 sizeof(数据类型)
#include<stdio.h>
int main()
{
     int a=10;
     int size = sizeof (a或10.9或10或double或char或int或float);
     printf(“size=%d ”,size);//答案为4,8,4,8,1,4,4
     return 0;
}
二、strlen
1.计算字符串长度(不会计算在内,有几个字符就算几个)
//计算的是字符数,不是字数
//一个中文占用3个字符
//
#include<stdio.h>
#include<string.h>
int main()
{
     int size=strlen(“haha哈哈”);
     printf(“%d ”,size);//答案为10=4+3*2;
     return 0;
}
 
三、结构体
数组:只能由多个相同类型的数据构成
结构体:可以由多个不同类型的数据构成 
int main()
{
//1.定义结构体类型
int main()
{
struct Person
{
     int age;//年龄
      double height;//身高
     char *name;//姓名或者用char name[10];
}; 
//2.根据结构体类型,定义结构体变量
struct Person p={20,1.85,”MosinNagant“};
   p.age=30;//更改数据
   p.name=“Mike”; 
 
struct Person p2;//错误的写法!不能分开赋值,只能在一起的时候
p2={21,1.72,”Jack”};
 
struct person p3={.height=1.77,.age=24,.name=“Jack”};//正确
return 0;
}
例子:
struct Date
{
     int year;
     int month;
     int day;
};
struct Date d1={2011,4,10};
struct Date d2={2022,5,19};
d1=d2;//对应的赋值给d1,
d2.day=22;
printf(“%d-%d-%d ”,d1.year,d1.month,d1.day);//2022-5-19
printf(“%d-%d-%d ”,d2.year,d2.month,d2.day);//2022-5-22
☆☆定义方法2☆☆
struct Student
{
     int age;
     double height;
     char *name;
}stu1={…},stu2={...};//???不能结构体雷属性重复定义,也就是不能有struct Student stu2!
定义方式3
struct{                   //匿名结构体
     int age;
     char *name;
}stu1={...},stu2={...};//????弊端更多,不能重复使用,不常用
*******重点:作用域
struct Student{int …;int...};放到main()前面的时候,之后都可以用;
放到自定义函数前同样可以使用;
结构体数组
int main()
{
     struct RankRecord
     {
          int no;
          int score;
          char *name;
     };
     struct RankRecord record[3]={
          {1,”jack”,5000},
          {2,”jim”,3000},
          {3,”rose”,200},
     };
     record[0].score=8000;//修改
     for(int i=0,i<3,i++)
     {
          printf(“%d %s %d ”,record[i].no,record[i].name,record[i].score);
     }
 
return 0;
}
 
***********
//结构体的嵌套:
#include <stdio.h>
void STRUCT()
{
    struct Point
    {
        int x;
        int y;
    };
    struct Size
    {
        double height;
        double wight;
    };
    struct Frame
    {
        struct Point p1;
        struct Size s1;
    };
    struct View
    {
        char *color;
        struct Frame f1;
    };
    struct View v1={"yellow",{{120,50},{80,60}}};
    printf("color:%s,x=%d,y=%d,height=%lf,wight=%lf ",v1.color,v1.f1.p1.x,v1.f1.p1.y,v1.f1.s1.height,v1.f1.s1.wight);
}
int main(int argc, const char * argv[])
{
//例题1:
    STRUCT();
//例题2:
    struct Birthday
    {
        int year;
        int month;
        int day;
    };
    struct Student
    {
        int age;
        float height;
        char *name;
        struct Birthday bir1;
    };
    struct Student stu1={23,175.2,"JACK",{2015,10,22}};
    stu1.age=20;
    printf(" This is:%d,%.2f,%s,Birthday:%d-%d-%d ",stu1.age,stu1.height,stu1.name,stu1.bir1.year,stu1.bir1.month,stu1.bir1.day);

    return 0;
}
答案:
color:yellow,x=120,y=50,height=80.000000,wight=60.000000

This is:20,175.20,JACK,Birthday:2015-10-22
 
原文地址:https://www.cnblogs.com/liuyingjie/p/4940912.html