0801学习笔记(内存操作相关函数)

内存操作相关函数

        复制函数  memcpy(目的地址,来源地址,需要拷贝的字符数量)

#include <stdio.h>
#include <string.h>
void print(int *p,int n);

void print(int *p,int n)
{
	int i;
	for(i=0;i<n;i++)
		printf("p[%d]=%d\n",i,*(p+i));
	printf("\n");
}

int main()
{
	int a[5]={2,5,8,9,10};
	int b[5]={0};
	
	memcpy(b,a,8);
	print(a,5);
//	printf("\n");
	print(b,5);
//	printf("\n");
	
	memcpy(a+2,a,8);//用0、1覆盖2、3 
	print(a,5);
	
	memcpy(a,a+3,8);//用3、4覆盖0、1 
	print(a,5);
	
	memcpy(a+1,a,32);//集体往后移一位,首位不变 
	print(a,5);
	
	memmove(b+3,b,8);//移动复制  参数2开始8字节 移动复制到参数1开始 
	print(b,5);
	
	int c[5]={0};
	print(c,5);
	memset(c,258,3);//258对应的十六进制数低位的1个字节补充3个字节  
	// 即02 02 02十六进制转换为十进制为131586 
	print(c,5);
	
	char num_0[5]={0};
	memset(num_0,'a',4);
	puts(num_0);
	
	int num_1[5]={10,22,15,31,64};
	int num_2[5]={10,12,11,55,44};
	int ret=0;
	ret=memcmp(num_1,num_2,8);//参数1、2是数组,参数3是比较的前n个字节  
	//相等输出0 左边大输出1 右边大输出-1 
	printf("%d\n",ret); 
	
	return 0;
} 

结果

p[0]=2
p[1]=5
p[2]=8
p[3]=9
p[4]=10

p[0]=2
p[1]=5
p[2]=0
p[3]=0
p[4]=0

p[0]=2
p[1]=5
p[2]=2
p[3]=5
p[4]=10

p[0]=5
p[1]=10
p[2]=2
p[3]=5
p[4]=10

p[0]=5
p[1]=5
p[2]=10
p[3]=2
p[4]=5

p[0]=2
p[1]=5
p[2]=0
p[3]=2
p[4]=5

p[0]=0
p[1]=0
p[2]=0
p[3]=0
p[4]=0

p[0]=131586
p[1]=0
p[2]=0
p[3]=0
p[4]=0

aaaa
1

--------------------------------
Process exited after 0.06155 seconds with return value 0
请按任意键继续. . .

关于空间

#include <stdio.h>
#include <stddef.h>
#include <string.h>
 

int main()
{
	int a[]={1,2,3,4};
	printf("%d\n",sizeof(a));//64-16  32-16  整个数组 
	printf("%d\n",sizeof(a+0));//64-8  32-4  指针(地址空间) 
	printf("%d\n",sizeof(*a));//64-4 32-4  首地址内容 
	printf("%d\n",sizeof(a+1));//64-8  32-4  指针(地址空间) 
	printf("%d\n",sizeof(a[1]));//64-4  32-4  第二元素内容 
	printf("%d\n",sizeof(&a));//64-8  32-4  取地址 
	printf("%d\n",sizeof(*&a));//64-16  32-16  整个数组 
	printf("%d\n",sizeof(&a+1));//64-8  32-4  指针(地址空间)
	printf("%d\n",sizeof(&a[0]));//64-8  32-4  指针(地址空间)
	printf("%d\n",sizeof(&a[0+1]));//64-8  32-4  指针(地址空间)
	printf("%#p\n",a);//a数组名是地址,指向数组首地址 
	printf("%#p\n",&a);//&a去数组名地址,也是地址,指向整行数组 
	printf("\n"); 
	
	char arr[]={'a','b','c','d','e'};
	printf("%d\n",sizeof(arr));//5
	printf("%d\n",sizeof(arr+0));//4/8
	printf("%d\n",sizeof(*arr));//1
	printf("%d\n",sizeof(arr[1]));//1
	printf("%d\n",sizeof(&arr));//4/8
	printf("%d\n",sizeof(&arr+1));//4/8
	printf("%d\n",sizeof(&arr[0]+1));//4/8
	printf("\n");
	
//	printf("%d\n",strlen(arr)); //随机值,需要'\0' 
//	printf("%d\n",strlen(arr+0));//随机值,需要'\0'
	printf("%d\n",strlen(*arr));//非法 
	printf("%d\n",strlen(arr[1]));//非法 
//	printf("%d\n",strlen(&arr));//随机值,需要'\0'
//	printf("%d\n",strlen(&arr+1));//随机值,需要'\0'
//	printf("%d\n",strlen(&arr[0]+1));//随机值,需要'\0'

	char ar[]="abcdf";
	printf("%d\n",sizeof(ar));//数组所占空间6
	printf("%d\n",sizeof(ar+0));//8/4 元素地址 
	printf("%d\n",sizeof(*ar));//1  内容 
	printf("%d\n",sizeof(ar[1]));//1  内容 
	printf("%d\n",sizeof(&ar));//8/4  整行地址 
	printf("%#p\n",&ar);//首行地址 
	printf("%d\n",sizeof(&ar+1));//8/4  ar整行地址移动一次  实际移动整行长度6 
	printf("%#p\n",&ar+1);//整行移动一次后的地址 
	printf("%d\n",sizeof(&ar[0]+1));//8/4  ar【0】地址移动一次  指向下一元素 
	printf("\n");
	
	printf("%d\n",strlen(ar));//数组所占空间5
	printf("%d\n",strlen(ar+0));//5 
//	printf("%d\n",strlen(*ar));//非法
//	printf("%d\n",strlen(ar[1]));//非法 
//	printf("%d\n",strlen(&ar));//5  
	printf("%#p\n",&ar);//首行地址 
//	printf("%d\n",strlen(&ar+1));//0  随机值 
	printf("%#p\n",&ar+1);//整行移动一次后的地址 
	printf("%d\n",strlen(&ar[0]+1));//4   可寻 
	
	printf("\n二维数组\n");
	int b[3][4]={0};
	printf("%d\n",sizeof(b)); //48  数组空间 3*4*4 
	printf("%d\n",sizeof(b[0][0])); //4  单个元素空间 
	printf("%d\n",sizeof(b[0])); //16  整行 空间 
	printf("%d\n",sizeof(b[0]+1)); //8/4  下行0元素地址 
	printf("%d\n",sizeof(*(b[0]+1))); //4  b[0][1]元素空间 
	printf("%d\n",sizeof(b+1)); //8/4  首地址+1 
	printf("%#p\n",b);//首地址 
	printf("%#p\n",b+1);//指向下一行 
	printf("%d\n",sizeof(*(b+1))); //16  整行空间 
	printf("%d\n",sizeof(&b[0]+1)); //8/4  行地址+1 
	printf("%d\n",sizeof(*(&b[0]+1))); //16  行地址+1取内容  整行内容空间 
	printf("%d\n",sizeof(*b)); //16  行地址取内容  整行空间 
	printf("%d\n",sizeof(b[3])); //16  跨度指定  整行空间 

	return 0;
} 

64位结果

16
8
4
8
4
8
16
8
8
8
0X000000000062FE00
0X000000000062FE00

5
8
1
1
8
8
8

6
8
1
1
8
0X000000000062FDE0
8
0X000000000062FDE6
8

5
5
0X000000000062FDE0
0X000000000062FDE6
4

二维数组
48
4
16
8
4
8
0X000000000062FDB0
0X000000000062FDC0
16
8
16
16
16

--------------------------------
Process exited after 0.09016 seconds with return value 0
请按任意键继续. . .

32位结果

16
4
4
4
4
4
16
4
4
4
0X0061FE90
0X0061FE90

5
4
1
1
4
4
4

6
4
1
1
4
0X0061FE85
4
0X0061FE8B
4

5
5
0X0061FE85
0X0061FE8B
4

二维数组
48
4
16
4
4
4
0X0061FE54
0X0061FE64
16
4
16
16
16

--------------------------------
Process exited after 0.07598 seconds with return value 0
请按任意键继续. . .

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值