zoukankan      html  css  js  c++  java
  • C++基础--struct的大小

    在修改别人的代码的过程中,发现很多人会把struct和struct的定义混淆,在这里主要是为了提醒自己Struct定义的规范性。

    #include <stdio.h>
    
    struct x{
    	int a;
    	char b;
    };
    
    typedef struct g{
    	int a;
    	char b;
    }G;
    
    int main()
    {
    	int a;
    	char b;
    	int m = sizeof(a);
    	int n = sizeof(b);
    	printf("size of int is %d
    ", m);
    	printf("size of char is %d
    ", n);
    	int x = sizeof(x);
    	int y = sizeof(G);
    	printf("size of struct is %d
    ", x);
    	printf("size of struct G is %d
    ", y);
    	return 0;
    }
    

      运行的结果为:

    在这里Struct G的大小为8是因为:字节对齐,说明Struct在默认情况下,就已经做了字节对齐。

  • 相关阅读:
    模拟--北京标准时间
    DOM方法
    Document-对象属性和常用的对象方法
    struts2标签
    OGNL
    Java基础方面
    初识拦截器
    访问者模式
    备忘录模式
    门面模式
  • 原文地址:https://www.cnblogs.com/anlia/p/Struct.html
Copyright © 2011-2022 走看看