基本架構
//
struct point //例如,我們假設有一個點,叫point,並且宣告這個point結構內有兩個變數,分別為a、b
{
int a;
int b;
};
程式範例1
#include <stdio.h>
#include <stdlib.h>
struct point //例如,我們假設有一個點,叫point,並且宣告這個point結構內有兩個變數,分別為a、b
{
int a;
int b;
};
int main()
{
struct point x = { 3, 5};
printf("point coordinate = ( %d, %d)\n", x.a, x.b);
system("pause");
return 0;
}
程式範例2
#include <stdio.h>
#include <stdlib.h>
typedef struct point
{
int a;
int b;
}point;
struct line
{
point head;
point tail;
};
int main()
{
struct line x = { 1, 2, 3, 4};
printf("line coordinate head = ( %d, %d)\n", x.head.a, x.head.b);
printf("line coordinate tail = ( %d, %d)\n", x.tail.a, x.tail.b);
system("pause");
return 0;
}
程式範例2結構語法剖析(以下方法都是等價的)
<方法一>
struct point
{
int a;
int b;
};
struct line
{
struct point head;
struct point tail;
};
<方法二>
struct point
{
int a;
int b;
};
typedef struct point point;
struct line
{
point head;
point tail;
};
<方法三>
typedef struct point
{
int a;
int b;
}point;
struct line
{
point head;
point tail;
};
範例2剖析說明
/*
*<方法一>當自定結構point後,要使用point時,必須連同struct一起使用才行,即struct point
*<方法二>當我們使用typedef賦予struct point一個新的名稱point以後,之後要使用結構point,只要直接打point就可以了
*<方法三>直接將 方法二 中的
struct point
{
int a;
int b;
};
與
typedef struct point point;
合併成
typedef struct point
{
int a;
int b;
}point;
*/
沒有留言:
張貼留言