c语言基础常用字符串数组函数与结构体

字符串常用函数

printf("%d",sizeof(arr));//可查看数组arr所占用的内存(一个int占4位,char占一位)
  • strcpy(arr1,arr2);

将arr2中的内容拷贝到arr1中;arr1的内存需大于arr2

  • strcmp(arr1,arr2);

逐个比较,若arr1>arr2 返回1;

arr1=arr2 返回0;

arr1<arr2 返回-1

  • strcat(arr1,arr2)字符串拼接 目的字符串数组大于两个字符串总和;
  • strlen 从字符串开始位置扫描,直到遇到/0返回计数器值

结构体

//结构体定义及赋值 
#include<stdio.h> 
struct student 
{
	int ID;
	char name;
		
}stu;

int main()
{
	stu.ID=10;//‘.’就是“的”,意为stu里的ID
	stu.name= 'g';
	printf("%d %c",stu.ID,stu.name);
	}
//或者下面那种赋值方法
#include<stdio.h> 
struct student 
{
	int ID;
	char name;
		
};

int main()
{
	struct student stu = {10,'g'} 
	printf("%d %c",stu.ID,stu.name);
	}



#include<stdio.h> 

typedef struct student //为数据类型struct定义一个别名,struct与stu可以互用 
{
	int ID;
	char name;
}stu;
int main()
{
	int ID = 10;
	stu stt ={123,'a'};//定义一个stu(struct)类型的变量并赋值。 
	printf("%d %c\n",stt.ID,stt.name);
	stu* Pstu = &stt;
	Pstu->ID =666;//指针用箭头 。
	printf("%d %c",stt.ID,stt.name);
	}
	/*分量运算符左侧的运算对象只能是结构变量,不能是指针。
	指向运算符左侧的运算对象,只能是指向结构变量或结构数组的指针变量。
	如果指针变量Pstu已指向结构变量stt,则:
	var.成员 与Pstu-> 等价 

结构体数组:

#include<stdio.h> 
struct student 
{
	int ID,age; 
	
}stu[10];
int main()
{
	stu[0].ID=20;
	stu[0].age =18;
	stu[1].ID =15; 
	printf("%d %d",stu[0].ID,stu[0].age);
	}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值