在修改别人的代码的过程中,发现很多人会把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\n&q…
结构体和类有点类似 我们定义一个类的时候 是class 类名 定义结构体的时候是 struct 结构体名 结构体的写法 struct Point { // public int x=10; 这种写法错误 public int x ; public int y; public int X { get { return x; } set { x = value; } } public void SayHi() { Console.WriteLine("哈哈哈哈哈")…
网络协议里面,很可能遇到自定义的封包,对应到c里面的是 typedef struct _PackageHeader { int headerLen; int timeStamp; short cmd }; 为了保证单字节对齐,可以加上编译选项 #pragma pack(1) 可惜的是,golang里面,没有这样的编译选项,猥琐的可参考的山寨代码如下 package main import ( "encoding/binary" "fmt" ) type TestSt…