以下代码有结构体的各种定义方法,不再详细说明。
直接看代码:
//
// main.m
#import <Foundation/Foundation.h>
struct{
char * name;
int age;
char * sex;
int test;
}people1;
//定义的时候直接申请结构体变量people1,并且没有结构体名,意味着这个结构体只有在定义结构体的时候申请了一个结构体变量,以后也不能申请结构体变量了
struct{
char * name;
char * sex;
int age;
int test;
}people3;//看似和上面一样,是为了测试结构体内存占用情况,详见输出
struct People{
char * name;
char * sex;
int age;
}people2;//定义的时候直接申请变量people2
typedef struct Dog{//加上typedef 相当于 struct Dog == SubDog,用法如下main里面代码
char * name;
int age;
short test;
}SubDog;
typedef struct{//这里可是和上面Dog一样加上Cat,也可以不写
short test;
char * name;
int age;
}SubCat;
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"%lu", sizeof(int));
NSLog(@"%lu", s