当前位置:网站首页>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;
}边栏推荐
- Jarvis OJ Flag
- [wechat applet] read the life cycle and route jump of the applet
- SQL injection of cisp-pte (Application of secondary injection)
- 【机器人坐标系第一讲】
- 齐宣王典故
- Jarvis OJ Telnet Protocol
- Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
- 高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
- Google Earth engine (GEE) -- a brief introduction to kernel kernel functions and gray level co-occurrence matrix
- 国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
猜你喜欢

阈值同态加密在隐私计算中的应用:解读

Cs231n notes (bottom) - applicable to 0 Foundation

Enter a command with the keyboard

二叉树相关OJ题

Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely

American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
![[61dctf]fm](/img/22/3e4e3f1679a27d8b905684bb709905.png)
[61dctf]fm

【刷题篇】鹅厂文化衫问题

Jarvis OJ Telnet Protocol

【刷题篇】有效的数独
随机推荐
启牛商学院股票开户安全吗?靠谱吗?
【729. 我的日程安排表 I】
手机开证券账户安全吗?怎么买股票详细步骤
Jarvis OJ 远程登录协议
Sentinel-流量防卫兵
How to set the WiFi password of the router on the computer
【剑指 Offer】61. 扑克牌中的顺子
[Jianzhi offer] 63 Maximum profit of stock
【729. 我的日程安排錶 I】
浏览器渲染原理以及重排与重绘
Solution of vant tabbar blocking content
C# TCP如何设置心跳数据包,才显得优雅呢?
数据访问 - EntityFramework集成
Sentinel flow guard
Data access - entityframework integration
Do sqlserver have any requirements for database performance when doing CDC
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
[729. My schedule I]