编程之家收集整理的这篇文章主要介绍了C语言入门-结构类型,编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>
int main(int argc,char const *argv[])
{
// 声明结构类型
struct date
{
int month;
int day;
int year;
};
// 使用自定义的类型
struct date today;
today.month = 9;
today.day = 30;
today.year = 2019;
printf("Today is date is %i-%i-%i\n",today.year,today.month,today.day);
return 0;
}
#include <stdio.h>
struct date
{
int month;
int day;
int year;
};
int main(int argc,char const *argv[])
{
struct date today;
today.month = 9;
today.day = 30;
today.year = 2019;
printf("Today is date is %i-%i-%i\n",today.day);
return 0;
}
struct point{
int x;
int y;
}
struct point p1,p2;
// 其中p1和p2都是point
// 里面有x和y的值
还有另外一种形式:
struct{
int x;
int y
}p1,p2 ;
// p1和p2都是一种无名结构,里面有x和y
当然了,还有一种更加常用的形式
struct point {
int x;
int y;
}p1,p2;
p1和p2都是point,里面有x和y的值
struct date{
int month;
int day;
int year;
};
int main(int argc,char const *argv[])
{
struct date taday = {9,30,2019};
struct date thismonth = {.month=9,.year=2019};
printf("Today is date is %i-%i-%i\n",taday.year,taday.month,taday.day );
printf("This month is %i-%i-%i\n",thismonth.year,thismonth.month,thismonth.day);
return 0;
}
// Today is date is 2019-9-30
// This month is 2019-9-0
int numberOfDays(struct date d);
#include <stdio.h>
struct point
{
int x;
int y;
};
void getStruct(struct point p);
void output(struct point p);
int main()
{
struct point y = {0,0};
getStruct(y);
output(y);
return 0;
}
void getStruct(struct point p){
scanf("%d",&p.x);
scanf("%d",&p.y);
printf("%d,%d\n",p.x,p.y);
}
void output(struct point p)
{
printf("%d,p.y);
}
// 2
// 3
// 2,3
// 0,0
#include <stdio.h>
struct point
{
int x;
int y;
};
struct point getStruct(void);
void output(struct point p);
int main()
{
struct point y = {0,0};
y = getStruct();
output(y);
return 0;
}
// 这里返回一个struct
struct point getStruct(void){
struct point p;
scanf("%d",p.y);
return p;
}
void output(struct point p)
{
printf("%d,p.y);
}
// 5 6
// 5,6
// 5,6
struct date
{
int month;
int day;
int year;
}myday;
struct date *p = &myday;
(*p).month = 12;
p->month = 12;
用->表示指针所指的结构变量中的成员
struct point
{
int x;
int y;
};
struct point* getStruct(struct point *p);
void output(struct point p);
void print(const struct point *p);
int main(int argc,char const *argv[])
{
struct point y = {0,0};
getStruct(&y);
output(y);
output(*getStruct(&y));
print(getStruct(&y));
return 0;
}
struct point* getStruct(struct point *p)
{
scanf("%d",&p->x);
scanf("%d",&p->y);
printf("%d,p->x,p->y);
return p;
}
void output(struct point p)
{
printf("%d,p.y);
}
void print(const struct point *p)
{
printf("%d,p->y);
}
以上是编程之家为你收集整理的C语言入门-结构类型全部内容,希望文章能够帮你解决C语言入门-结构类型所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入编程之家官方QQ群:1065694478