结构体

结构体

 声明一个结构体类型的一般形式为:

struct    结构体名   {成员表列}

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
    char name[20];
    int  age;
    char sex;
    char z;
};

int main()
{
   /* struct student s1={"aaa",10,'m'};
    struct student s2;
    scanf("%s%d %c",s2.name,&s2.age,&s2.sex);
    printf("%s %d %c\n",s1.name,s1.age,s1.sex);
    printf("%s %d %c\n",s2.name,s2.age,s2.sex);*/
   /* struct student *s3=(struct student*)malloc(sizeof(struct student));
    strcpy(s3->name,"aaa");
    s3->age=10;
    s3->sex='m';
    printf("%s %d %c\n",s3->name,s3->age,s3->sex);*/
    printf("%d",sizeof(struct student));
    
    return 0;
}

 

#include<stdio.h>
#include<stdlib.h>

struct student
{
    char name[20];
    int age;
    char sex;
};

int main()
{
    int i;
    struct student *stu[5];

    for(i=0;i<5;i++)
    {
        stu[i]=(struct student*)malloc(sizeof(struct student));
        scanf("%s%d %c",stu[i]->name,&stu[i]->age,&stu[i]->sex);
    }

    for(i=0;i<5;i++)
    {
        printf("%s%d %c",stu[i]->name,stu[i]->age,stu[i]->sex);
    }
    return 0;
}

//1.结构体的总长度一定是最长成员的整数倍;

//2.每个成员的偏移量,一定是该成员长度的整数倍。

内存管理 
系统会为一个进程分配4GB的虚拟内存,通常情况下,1GB为内核态,由内核使用,3GB为用户态,用户态由以下5部分组成 
数据段:存放全局变量、static静态变量 
代码段:存放代码、常量,为只读模式 
栈空间:存放局部变量 
.堆空间:mollac申请,free释放 
(进程间共享的内存通信等)

3.堆和栈的区别 
栈:操作系统管理,申请释放都是操作系统完成 
堆:用户管理,申请(malloc)和释放(free)由用户完成

#include<stdio.h>
#include<stringh>
#include<stdlib.h>

int global=0;
char *p1;

int main()
{
    int a;
    char s[]; //s++ 错误;(*a)++正确;数组是常指针。
    char *p2;
    char *p3="123456789";//只读  p3 ++正确; (*p3 )++;错误 
    static int c=0;

    p1=(char *)malloc(100);

    strcpy(p1,"123456789");

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值