江红之乡

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1:mallo内存分配
#include"stdio.h"
#include"string.h"
#include "malloc.h"
void main()
{
char *src ="hello world"; //数据大小 11=10+1空格
char *dst=NULL;
dst=(char *)malloc(sizeof(char)*strlen(src)+1);//分配内存时候的注意事项:对应类型的转换,数据大小和
memcpy(dst,src,strlen(src));
printf("dst=%c\r\n",*dst);
printf("src=%c\r\n",*src);
printf("sizeofchar=%d\r\n",sizeof(char));
printf("sizeofchar=%d\r\n",strlen(src));
printf("sizeofchar=%d\r\n",sizeof(char*));
}

 

dst=h
src=h
sizeofchar=1
sizeofchar=11
sizeofchar=4

 

2:常量指针变量:
#include"stdio.h"
#include"string.h"
void main()
{
int num1=10;
int num2=10;
int * const ptr1=&num1;//必须在这地方有一个指向,否则报错 指向的地址不可改变,ptr1=&num2 代表一个常量 ,
//但是num1的数值是可以改变的 比如num1 =100,*ptr1=1000; 只是指向地址不能改变
printf("value=%d\r\n",*ptr1);
}
其实是指向的地址不可改变 为一个常量 其他可以改变 可用 通过num1=1000;或者*ptr1=1000来改变数值
常量指针:

#include"stdio.h"
#include"string.h"
void main()
{
int num1=10;
int num2=10;
const int * ptr1=&num1;//修饰的是(int * ptr1) 所以//*ptr1=1000; 这个是不能操作成功的 已经固定为常量而已 不能通过*ptr1改变 其他可以操作
//可以改变初始化的地址,
printf("value=%d\r\n",*ptr1);
num1=100;
printf("value=%d\r\n",*ptr1);
}


int const * const a6 = &a1; ///const data,const pointer
const int * const a7 = &a1; ///const data,const pointer
只能修改a1的数值

 

3:函数指针

#include"stdio.h"
#include"string.h"
#include "malloc.h"
void mesg(int num)
{
printf("mesg no.%d\r\n",num);

}
int* add(int x,int y)
{
int *z=(int *)malloc(sizeof(int));
*z=10;
printf("z=%d,x+y=%d\r\n",*z,x+y);
return z;
}
int one(int data)
{

printf("data=%d\r\n",data+1);
printf("one\r\n");
}

int two(int data)
{

printf("data=%d\r\n",data+2);
printf("two\r\n");
}

int three(int data)
{

printf("data=%d\r\n",data+3);
printf("three\r\n");
}

 


int serach(int(*all)(int),int data)
{

return (*all)(data);
}

int (*p[])(int)={one,two,three};
typedef int (*calc)(int y);
calc remathfunc(int index)
{
return p[index];

}


void main()
{
int *t;
void (*fpmsg)(int);
int* (*addfptr)(int ,int );
int (*calcc)(int x);
calcc=remathfunc(0);

addfptr=&add;
fpmsg=&mesg;
addfptr(10,100);
fpmsg(100000);
serach(one ,10);
serach(two ,10);
serach(three ,10);
calcc(10000);
}

7,1 Top

 

 

 

#include"stdio.h"
#include"string.h"
#include "malloc.h"
void mesg(int num)
{
printf("mesg no.%d\r\n",num);

}
int* add(int x,int y)
{
int *z=(int *)malloc(sizeof(int));
*z=10;
printf("z=%d,x+y=%d\r\n",*z,x+y);
return z;
}

void main()
{
int *t;
void (*fpmsg)(int);
int* (*addfptr)(int ,int );
addfptr=&add;
fpmsg=mesg;
addfptr(10,100);
fpmsg(100000);


}
}
int* add(int x,int y)
{
int *z=(int *)malloc(sizeof(int));
*z=10;
printf("z=%d,x+y=%d\r\n",*z,x+y);
return z;
}

void main()
{
int *t;
void (*fpmsg)(int);
int* (*addfptr)(int ,int );
addfptr=&add;
fpmsg=mesg;
addfptr(10,100);
fpmsg(100000);

 

 

 

#include"stdio.h"
#include"string.h"
void main()
{
int arr[]={1,2,3,4};
int (*ptr2arr)[4];
int i=0;
int *ptrr=arr;
ptr2arr=&arr;
for(i=0;i<4;i++)
{
printf("address of arrdar=%p\r\n",arr+i);
}
for(i=0;i<4;i++)
{
printf("address of %p=%d\r\n",(ptr2arr[0]+i),*(ptr2arr[0]+i));
}
}


address of arrdar=0xbfc53b90
address of arrdar=0xbfc53b94
address of arrdar=0xbfc53b98
address of arrdar=0xbfc53b9c
address of 0xbfc53b90=1
address of 0xbfc53b94=2
address of 0xbfc53b98=3
address of 0xbfc53b9c=4

//////****************************************************************/////
char *p1[10];
char (*p2)[10];
int i=0;

char a1[10]={'1','2','3','4','5','6','7','8','9','\0'};
char a2[2][10]={{'q','w','e','r','t','y','u','i','o','\0'},{'a','s','d','f','g','h','j','k','l','\0'}};
p1[0]=&a1[0];
p1[1]=&a1[1];
p1[8]=&a1[8];
printf("%c\n",*p1[0]);
printf("%c\n",*p1[1]);
printf("%c\r\n",*p1[8]);
p2=a2;

for(i=0;i<10;i++)
{
printf("%c\r\n",p2[0][i]);
printf("%c\r\n",p2[1][i]);
}

1
2
9
q
a
w
s
e
d
r
f
t
g
y
h
u
j
i
k
o
打印结果 实际为操作一维数组 的理解方式即可

 

posted on 2019-02-21 14:03  江红之乡  阅读(1058)  评论(0编辑  收藏  举报