一、结构体存储

#include<stdio.h>
#include<stdlib.h> struct info{
char c; //1 2 4 8
double num; //1 2 4 8 char short int double
char ch[]; //9 10 12 16 }; void main() {
printf("%d\n",sizeof(struct info));
struct info in={'a',5.2,"hello"};
printf("%p\n",&in);
printf("%p\n",&in.c);
printf("%p\n",&in.num);
printf("%p\n",&in.ch); system("pause");
}

二、枚举类型(限定取值)

枚举常量实质是整型数据

#include<stdio.h>
#include<stdlib.h> //枚举的一般形式,限定在这个范围内取值
//如果没有一个赋初值,就会从0循环到最后一个,每次加1
//如果第一个赋初值,后面每个加1
//除非自己赋值,否则计算机赋值会让每个枚举常量都不同
enum level{
司令=,军长=,师长,旅长,团长,营长,连长,排长,班长,士兵
}; void main() {
enum level l1=司令;
enum level l2=军长;
enum level l3=师长;
printf("%d\n",l1);
printf("%d\n",l2);
printf("%d\n",l3); system("pause");
}

三、typedef

#include<stdio.h>
#include<stdlib.h> typedef int aa; //typedef没有创建数据类型,给已经有的数据类型起一个别名.编译时处理,仅适用于类型
#define zhengshu num //define是替换,预处理,适用于任何场合 void main() {
aa zhengshu=;
printf("%d\n",num);
system("pause");
}

#include<stdio.h>
#include<stdlib.h> typedef int I;//给int一个别称
typedef int* IP;
void main() {
I num=;//int num=100
IP p=&num;//int *p=&num
printf("%d,%d\n",num,*p);
printf("%p,%p\n",&num,p);
system("pause");
}

#include<stdio.h>
#include<stdlib.h> void main() {
/*int a[10];
int s[10];*/
typedef int s[];//重定义数组类型
s x;
for (int i = ; i < ; i++)
{
x[i]=i;
printf("%d\n",x[i]);
}
system("pause");
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h> struct info{
char name[];
int num;
}; typedef struct info I;//I=struct info
typedef struct info* P;//P=struct info* void main() {
I s;
strcpy(s.name,"yincheng");
s.num=;
printf("%s,%d\n",s.name,s.num); P ip=(P)malloc(sizeof(I));
strcpy(ip->name,"yincheng8848");
ip->num=;
printf("%s,%d\n",(*ip).name,ip->num);
  free(ip); system("pause");
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h> typedef struct tel{
char name[];
long long num;
}T,*P; void main() {
printf("%d\n",sizeof(struct tel));
printf("%d\n",sizeof(T));
printf("%d\n",sizeof(P)); T t1;
strcpy(t1.name,"yincheng");
t1.num=;
printf("%s,%lld\n",t1.name,t1.num); P pt1=(P)malloc(sizeof(T));//malloc之后记得要free
strcpy(pt1->name,"尹成");
pt1->num=;
printf("%s,%d\n",(*pt1).name,pt1->num);
free(pt1); system("pause");
}

四、深拷贝和浅拷贝

#include<stdio.h>
#include<stdlib.h>
#include<string.h> typedef struct string{
char *p;
int len;
}S; //浅拷贝,共享内存
void main1() {
S s1,s2;
s1.len=;
s1.p=(char *)malloc(sizeof(char)*);
strcpy(s1.p,"hello");
printf("s1.p=%s\n",s1.p);
s2.len=s1.len;
s2.p=s1.p;
*(s1.p)='K';
printf("s2.p=%s\n",s2.p); system("pause");
}
//深拷贝,拷贝内存内容
void main() {
S s1,s2;
s1.len=;
s1.p=(char *)malloc(sizeof(char)*);
strcpy(s1.p,"hello");
printf("s1.p=%s\n",s1.p);
s2.len=s1.len;
s2.p=(char *)malloc(sizeof(char)*);
strcpy(s2.p,s1.p);
*(s1.p)='K';
printf("s2.p=%s\n",s2.p); system("pause");
}

浅拷贝

深拷贝

[c/c++] programming之路(28)、结构体存储和内存对齐+枚举类型+typedef+深拷贝和浅拷贝的更多相关文章

  1. C 语言结构体 struct 及内存对齐

    struct 结构体 对于复杂的数据类型(例如学生.汽车等),C 语言允许我们将多种数据封装到一起,构成新类型. 跟面向对象语言中的对象相比,结构体只能包含成员变量,不支持操作. #include & ...

  2. C++ 自定义结构体和类 内存对齐

    为什么要提出内存对齐? 比如这么一种处理器,它每次读写内存的时候都从某个8倍数的地址开始,一次读出或写入8个字节的数据,假如软件能保证double类型的数据都从8倍数地址开始,那么读或写一个doubl ...

  3. C/C++ 结构体成员在内存中的对齐规则(转载)

    这几天在看王艳平的<windows 程序设计>,第5章讲解了MFC框架是怎么管理窗口句柄到窗口实例之间的映射,用到了两个类CPlex和CMapPtrToPtr,用于管理内存分配的类(避免因 ...

  4. C/C++ 结构体成员在内存中的对齐规则

    这几天在看王艳平的<windows 程序设计>,第5章讲解了MFC框架是怎么管理窗口句柄到窗口实例之间的映射,用到了两个类CPlex和CMapPtrToPtr,用于管理内存分配的类(避免因 ...

  5. Delphi 给结构体指针分配内存,用new(p),释放用dispose(p)

    来自:http://blog.163.com/zhangzhifeng688%40126/blog/static/1652627582010102261748481/ 给结构体指针分配内存  但在很多 ...

  6. C++中的结构体所占内存空间总结

    因为结构体有时候需要字节对齐.一般而言,struct 的 sizeof 是所有成员字节对齐后长度相加,而 union 的 sizeof 是取最大的成员长度. 在默认情况下,编译器为每一个变量或数据单元 ...

  7. 【C/C++】【VS开发】结构体存储空间数据对齐说明

    关于内存对齐 一: 1.什么是内存对齐 假设我们同时声明两个变量: char a; short b; 用&(取地址符号)观察变量a, b的地址的话,我们会发现(以16位CPU为例): 如果a的 ...

  8. C/C++编程笔记:C语言对齐问题【结构体、栈内存以及位域对齐】

    引言 考虑下面的结构体定义: 假设这个结构体的成员在内存中是紧凑排列的,且c1的起始地址是0,则s的地址就是1,c2的地址是3,i的地址是4. 现在,我们编写一个简单的程序: 运行后输出: 为什么会这 ...

  9. c语言学习笔记之结构体存储

    今天讲讲结构体存储问题 首先,结构体简单说是对不同类型的封装,一开始我们可能会想结构体在内存中的存储的大小是直接元素的和 例如 我们可能会觉得是 结构体大小=int(4个字节)+ short(2个字节 ...

随机推荐

  1. django创建命令及配置

    创建项目django-admin startproject XXX(项目名字)运行项目 python manage.py runserver创建子应用python manage.py startapp ...

  2. 安装_oracle11G_客户端_服务端_链接_oracle

    在开始之前呢,有一些注细节需要注意,oracle11G_客户端_和_服务端, 分为两种   一种是  开发者使用    一种是  BDA  自己使用(同时也需要根据自己 PC 的系统来做_win7_与 ...

  3. Xamarin打包

  4. WinAPI 字符及字符串函数(9): lstrcat - 合并字符串

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  5. NodeJS笔记(六)-Express HTTP服务器启动后如何关闭

    npm start启动网站,提示“3000”端口已经被使用的问题 nodejs WEB服务器并不随着cmd的关闭而终止 查看任务管理器可以看到nodejs的启动进程 可以手动关闭 如果是一直处于cmd ...

  6. 洛谷P4456 交错序列[CQOI2018] dp+数论

    正解:dp 解题报告: 传送门 首先可以先拆下这个贡献式,为了方便之后设状态什么的,把式子转成和ny有关,就成了 \(\sum \left ( n-i \right )^{a}\cdot i^{b}\ ...

  7. kubernetes安装

    本文主要参考自: https://blog.csdn.net/real_myth/article/details/78719244 还有一份更适合在生产环境使用的超强高可用(多master,nginx ...

  8. 使用tortoisegit工具git地址中带号码密码的拉取,以及使用这种方式后中途重置密码报git remote: HTTP Basic: Access denied 错误解决办法

    1. 在拉取git项目时可以在地址中直接指定号码密码如下就可以直接拉取下来 https://username:password@github.com   需要注意,因为在解析地址时是以@符号作为地址信 ...

  9. SpringBoot项目修改html后不即时编译

    springboot templates 下的 html 修改后无法达到即时编译的效果,搜索资料后记录笔记.原文地址:https://www.cnblogs.com/jiangbei/p/843939 ...

  10. mysql扩展性架构实践N库到2N 库的扩容,2变4、4变8

    mysql扩展性架构实践N库到2N 库的扩容,2变4.4变8 http://geek.csdn.net/news/detail/5207058同城 沈剑 http://www.99cankao.com ...