当前位置:网站首页>Little knowledge about C language (array and string)
Little knowledge about C language (array and string)
2022-07-05 17:01:00 【Oh-liuxing】
(1)memset(a,0,sizeof(a)) The function of is to put the array a Zero clearing , It's also in string.h In the definition of . Use memset Than for Circulation is more convenient 、 quick .
#include <stdio.h>
#include<string.h>
int main () {
int a[5];
memset(a,0,sizeof(a));
return 0;
}
(2) If from array a Copied to the k Elements to arrays b, You can use this statement :memcpy(b,a,sizeof(int)*k).
Use memcpy The function contains the header file string.h. If you need to put the array a Copy all to the array b in , It can be written. :memcpy(b,a,sizeof(a)).
#include <stdio.h>
#include<string.h>
int main () {
int a[]={1,2,3,4,5};
int b[5];
memcpy(b,a,sizeof(a));
return 0;
}
(3) stay stdlib.h There's a function in atoi, Its function prototype is as follows :int atoi(char *s)
It means that the string s Convert the contents of into an integer number and return , Such as a string “1234”, Then the return value of the function is 1234.
#include <stdio.h>
#include<stdlib.h>
int main () {
printf("%d",atoi("1234"));
return 0;
}
(4) stay stdlib.h There's a function in itoa, It means that the integer value Convert to a string and store it in string.
for example ,itoa(32,string,10) Yes, it will 32 Become a string of decimal numbers “32”, And return a pointer to this string ;
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main () {
char a[10];
printf("%s",itoa(32,a,10));
return 0;
}
(5)sprintf It's a variable parameter function , The function of this function is to write the formatted data into a string , Its return value is string length . The header file containing this function is stdio.h.
for example , The function of this program is to convert integers 123 Print as string and store in character array a[ ] in .
#include <stdio.h>
int main () {
char a[10];
int len=sprintf(a,"%d",123);
printf("%d",len);
return 0;
}
(6)strchr The function of finding string is s First character in c The location of . Its return value is the first occurrence of the return c A pointer to the position of , If s Does not exist in the c Then return to NULL. The header file containing this function is string.h.
#include <stdio.h>
#include<string.h>
int main () {
char a[]={"hello word"};
char b='h';
char *c=strchr(a,b);
printf("%c",*c);
return 0;
}
边栏推荐
- 如何将mysql卸载干净
- Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
- Learnopongl notes (II) - Lighting
- Etcd 构建高可用Etcd集群
- 深耕5G,芯讯通持续推动5G应用百花齐放
- 【Web攻防】WAF检测技术图谱
- 中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
- 【刷題篇】鹅廠文化衫問題
- The difference between searching forward index and inverted index
- tf. sequence_ Mask function explanation case
猜你喜欢
How to install MySQL
养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
机器学习编译第2讲:张量程序抽象
Summary of methods for finding intersection of ordered linked list sets
阈值同态加密在隐私计算中的应用:解读
Learnopongl notes (II) - Lighting
Jarvis OJ Telnet Protocol
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
随机推荐
Benji Banas membership pass holders' second quarter reward activities update list
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
【性能测试】jmeter+Grafana+influxdb部署实战
微信公众号网页授权登录实现起来如此简单
项目引入jar从私服Nexus 拉去遇到的一个问题
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
Error in composer installation: no composer lock file present.
Facing new challenges and becoming a better self -- attacking technology er
Raspberry pie 4B installation pytorch1.11
DenseNet
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
Learnopongl notes (I)
Twig数组合并的写法
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
"21 days proficient in typescript-3" - install and build a typescript development environment md
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
Writing method of twig array merging
BS-XX-042 基于SSM实现人事管理系统
How to install MySQL
干货!半监督预训练对话模型 SPACE