示例
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
struct date
{
int month;
int day;
int year;
};
struct date1
{
int month;
int day;
int year;
} todaysDate, purchaseDate; //定义结构的同时声明两个变量
struct
{
int month;
int day;
int year;
} todaysDate1 = { 9, 25, 2014 }; //定义结构的同时声明变量并赋初值
struct
{
int month;
int day;
int year;
} dates[100]; //定义结构的同时声明数组
struct date today;
today.month = 9;
today.day = 25;
today.year = 2004;
// %.2i 强制显示两位数,例如 05
NSLog(@"Today's date is %i/%i/%.2i.", today.month,
today.day, today.year % 100);
// 初始化结构
struct date tomorrow = { 9, 26, 2004 };
struct date tomorrow1 = { 9 };
struct date tomorrow2 = { .month = 9, .day = 26, .year = 2004};
struct date tomorrow3 = { .year = 2004 };
/* 点 */
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
/* 宽和高的尺寸 */
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
/* 矩形 */
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
CGPoint startPt;
startPt.x = 100;
startPt.y = 200;
CGSize rectSize;
rectSize.width = 200;
rectSize.height = 100;
CGRect theFrame;
theFrame.origin = startPt;
theFrame.size = rectSize;
theFrame.size.width = 175;
theFrame.origin.x = 0.0;
theFrame.origin.y = 0.0;
}
return 0;
}