结构体
定义有三种:
#include <stdio.h>
struct date
{
int year;
int month;
int day;
};
//struct date today;
/*
// definition of structure method 2
struct
{
int year;
int month;
int day;
}today;
*/
/*
// definition of structure method 3
struct date
{
int year;
int month;
int day;
}today;
*/
int main(int argc, const char *argv[])
{
struct date tomorrow = {2015, 5, 13};
struct date today = {.year = 2015, .day = 12}; // Note: first defination and initialization can be work
// today = {.year = 2015, .day = 12}; // it didn't work
// today = tomorrow; // array can't be assignment each other
today.month = 5;
printf("Date of tomorrow: %d-%d-%d\n", tomorrow.year, tomorrow.month, tomorrow.day);
printf("Date of today: %d-%d-%d\n", today.year, today.month, today.day);
struct date *thisDay = NULL;
thisDay = &today;
thisDay->day++;
thisDay->year++;
thisDay->month++;
printf("*thisDay: %d--%d--%d\n ", thisDay->day, thisDay->month, thisDay->year);
return 0;
}
结构,又名结构体,在其结构中可以定义多种不同数据类型的变量,
数组呢,只能定义相同数据类型的变量,一经定义后,不可改变;数组名亦是地址,但结构名不是;结构在作函数参数时要借用相关结构指针操作;
#include <stdio.h>
struct point {
int x;
int y;
};
void swap(int a[], int i);
void setPoint(struct point *p1);
int main()
{
int a[] = {3, 12,54 , 5, 6};
struct point *p = NULL;
struct point mypoint = {2, 5};
p = &mypoint;
setPoint(p);
---------------- // ....... many codes loss
return 0;
}