一、结构体存储

#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. linux安装杀毒软件

    https://www.cnblogs.com/bingo1024/p/9018212.html

  2. 多线程之Executors基本使用

    Executors几种创建方式 https://www.cnblogs.com/yasong/p/9318522.html 线程池数如何设置 https://blog.csdn.net/u013276 ...

  3. org.hibernate.HibernateException: Duplicate identifier in table for: Waa

    提示表的标识符重复,发现是数据库中的主键id重复了.因为是序列自动生成的. 我原本以为是因为我的序列的问题,序列.nextval()有问题,但是当我在数据库测试时,发现当前序列没有问题.但是当数据插入 ...

  4. Get Random number

    , int pMaxVal = int.MaxValue) { int m = pMaxVal - pMinVal; int rnd = int.MinValue; decimal _base = ( ...

  5. OC获取ip地址

    +(NSString *)getIp{ NSError *error;NSURL *ipURL = [NSURL URLWithString:@"http://pv.sohu.com/cit ...

  6. ASM检查RAC是否成功

    [grid@asm ~]$ crsctl status resourceNAME=ora.DATA.dgTYPE=ora.diskgroup.typeTARGET=ONLINESTATE=ONLINE ...

  7. C语言之指针若干问题

    1.指针变量的赋值问题. 常常有偷懒的小伙子,这样赋值 int *Pointer =  3:/ 这是给Pointer 所指的变量赋值,刚创建Pointer时,它所指的变量是不固定的,可能是某个重要的系 ...

  8. python之进程,线程,协程简单理解

    进程:资源单位,由操作系统控制调度.正在执行的一个程序或者过程,进程之间不共享资源,进程间通讯手段:管道,队列,信号量等.多用于计算密集型场景,如金融计算 线程:是cpu的最小执行单位,由操作系统控制 ...

  9. windows 下面安装make

    1.前面文章中已经提到了wingw32的安装,安装好之后设置相应环境变量.2.打开cmd,输入 mingw-get install mingw32-make,会进行安装.3.输入 mingw32-ma ...

  10. jquery判断字符长度 数字英文算1字符 汉字算2字符

    <input type="text" maxlength="25" oninput="textlength(this)"> &l ...