C++ 中使用memset和memcpy 对字符串和字符串数组处理
来源:互联网 发布:java监听端口接收数据 编辑:程序博客网 时间:2024/05/17 09:27
#include <iostream>#include <string.h>using namespace std;struct SomeInfo{char id[30];char name[30];};struct TotalInfo{char total[20];SomeInfo my[10];};class myClass{public:myClass(){}~myClass(){} void memcopy(int ,TotalInfo);void print();private:TotalInfo m_totalInfo;int m_count;};void myClass::memcopy(int count ,TotalInfo info){m_count = count;memcpy(&m_totalInfo,&info,sizeof(m_totalInfo));}void myClass::print(){std::cout << m_totalInfo.total << std::endl;for (int i = 0; i != m_count ; ++i){ std::cout << m_totalInfo.my[i].id << std::endl; std::cout << m_totalInfo.my[i].name << std::endl;}}int main(){myClass here = myClass();TotalInfo totalInfo;memset(&totalInfo, 0, sizeof(totalInfo));char total[20] = "totalInfo.total"; memcpy(totalInfo.total,total,20); int count = 5;for (int i = 0; i != count ; ++i){char _id[30] = "totalInfo.Some.id";char _name[30] = "totalInfo.Some.name";memcpy(totalInfo.my[i].id, _id,sizeof(_id));memcpy(totalInfo.my[i].name, _name,sizeof(_name));}here.memcopy(count, totalInfo);here.print();return 0;}
在main函数的第三行,memset初始化新申请的内存。memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法[1]。
一共三个参数,地址,请零(目前做的是清零动作,而不是char型值),地址空间大小。
memcpy 也有三个参数,一个目标地址,源地址,和 大小。
- C++ 中使用memset和memcpy 对字符串和字符串数组处理
- C和指针之字符串memcpy、memmove、memset使用总结
- memset和memcpy使用教训
- C语言中memset ,memcpy 和strcpy 的区别
- C语言中memcpy、memset和memmove的实现
- SQL中对字符串操作和处理
- C数组和字符串
- C++:memset ,memcpy 和strcpy 的区别
- C语言memset、memcpy、memmove和strcpy
- C语言中的memcpy()和memset()函数
- C语言中字符串和字符数组
- memset、memcpy和strcpy函数使用
- strcpy、memset和memcpy的使用
- memset,memcpy,strcpy 的区别及C字符串函数
- memcpy 和 memset
- memset ,memcpy 和strcpy
- memcpy,memmove和memset
- memset和memcpy
- vim Sessions
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
- 宇龙酷派202笔试题
- 实现输出一个数字的二进制表示
- C语言小结(二)
- C++ 中使用memset和memcpy 对字符串和字符串数组处理
- ADC
- Linux下软件安装方法总结
- 关于全局变量在头文件中声明的问题 ctrl-c from凌阳—浩哥
- 【FFT-快速傅立叶变换】
- memcache 和 redis 的区别
- POJ 1833 排列 (STL)
- OC——dealloc方法(析构方法)
- 快速构建实时抓取集群