结构类型

结构体
定义有三种:

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值