2021-04-13

这篇博客详细介绍了C语言中的指针、数组和结构体的概念与使用。首先讲解了指针如何存储和访问变量地址,接着展示了如何通过指针操作数组,包括获取数组长度的方法。然后,介绍了结构体的基础语法,包括如何定义和初始化结构体,以及如何嵌套结构体。最后,讨论了结构体指针的使用,包括如何通过指针访问结构体成员,并演示了如何传递结构体变量给函数进行处理。
摘要由CSDN通过智能技术生成
一、指针
#include<stdio.h>
int main(){
	int a = 123;
	char b = 'b';
	// 指针的定义  指向地址
	int *pa = &a;
	char *pb = &b;
	
	// pa(pb) 为指针,指向a(b)的地址。
	//*pa(*pb) 是指向a(b)的值。
	
	printf("%d\t%c",*pa,*pb);	
	 
} 
二、数组指针
#include<stdio.h>
int main(){
	//指向数组的指针
	char name[] = "liuzhao";
	//定义一个指针并指向数组的地址 
	char *pname = name;
	printf("%s\n",pname);
	
	//判断数组字节数 
	int i= 0;
	while(*pname++ != '\0'){
		i++;
	}
	printf("一共%d个字节",i);
} 
三、结构体
1.结构体的基本语法
#include<stdio.h>

//struct 结构体关键字  注意有";"
struct Book{    
	char title[128];
	char author[40] ;
	unsigned int date;      // 打印时应该用"%u"打印;
}book;  //这里的book是全局变量 

int main(void){
    
	//局部变量
	//	struct Book book;   // 为结构体Book定义一个变量"book";
	//	book.author; 
	
	printf("请输入书名");
	scanf("%s",book.title);    //数组本身就是地址,无需加"&"; 
	printf("请输入作者");
	scanf("%s",book.author);
	printf("请输入日期");
	scanf("%d",&book.date);
	
	printf("数据录入完毕\n");
	
	//利用"."调用结构体中的数据; 
	printf("书名:%s\n",book.title);
	printf("作者:%s\n",book.author);
	printf("日期:%d\n",book.date);
	return 0;
	
}
2.结构体的嵌套
#include<stdio.h>

struct Date{
	int year;
	int month;
	int day;
};
struct Book{    
	char title[128];
	char author[40] ;
	struct Date date;   
}book = {
		"liuzhao",
		"刘小朝",
		{2021,04,14}
	};

int main(void){
	
	printf("%s\n",book.title);
	printf("%s\n",book.author);
	printf("%d-%d-%d\n",book.date.year,book.date.month,book.date.day);

	return 0;
}
 
3.结构体指针
#include<stdio.h>
struct Date{
	int year;
	int month;
	int day;
}; 
struct Book{
	char title[20];
	char author[20];
	struct Date date;
};
int main(void){
	
	struct Book book;
	struct Book *pbook;   //定义一个指向Book的指针; 
	pbook = &book;       
	printf("请输入作者:") ;
	scanf("%s",book.title); 
	printf("%s\n",pbook -> title);
	printf("%s\n",(*pbook).title);
	
	//(*结构体指针).成员名
	//结构体指针 -> 成员名 
	return 0;
}
传递结构体变量
#include<stdio.h>

struct Date{
	int year;
	int month;
	int day;
};
struct Book{
	char title[128];
	char author[40];
	float price;
	struct Date date;   //结构体的嵌套 
};
//函数的声明 
struct Book getInput(struct Book book);
void printBook(struct Book book);

struct Book getInput(struct Book book){
	printf("请输入书的名字:"); 
	scanf("%s",&book.title);
	printf("请输入作者:");
	scanf("%s",&book.author);
	printf("请输入本书的价格:");
	scanf("%f",&book.price);
	printf("请输入本书的出版日期:");
	scanf("%d%d%d",&book.date.year,&book.date.month,&book.date.day);
	
	return book; //返回到b1; 
}
void printBook(struct Book book){
	printf("书名:%s\n",book.title);
	printf("作者:%s\n",book.author);
	printf("价格:%f\n",book.price);
	printf("日期 :%d\n",book.date.year,book.date.month,book.date.day);
}

int main(void){
	
	struct Book b1,b2;  //定义Book结构体变量
	printf("请录入第一本书的信息");
	b1 = getInput(b1);  //将b1实参传递到14(struct Book book)行的形参中 
	putchar('\n');
	printf("请录入第二本书的信息");
	b2 = getInput(b2);
	printf("\n\n 录入完毕 \n\n");
	
	printf("输出第一本书的信息:");
	printBook(b1);
	putchar('\n');
	printf("输出第一本书的信息:");
	printBook(b2);
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值