Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

指针深入

指针的数组
本质是数组,数组中的每个元素都是指针
int *p[10];
占用的内存空间大小 = 指针大小 * 数组长度 = 8 * 10 = 80
p[0]~~~~p[9] 都是 int *

char pstr[8];//88 = 64

0 '\0' NULL
int main(int argc, const char * argv[]) {
char *pstr[5]={NULL};//把指针数组中的每个指针变量都初始化为NULL
for (int i=0; i<5; i++) {
pstr[i]=(char *)malloc(1000);
if (!pstr[i]) {
return -1;
}
scanf("%s", pstr[i]);
}

for (int i=0; i<5; i++) {
    printf("%s\n", pstr[i]);//%s占位符,对应跟字符串首地址
}
return 0;

}

char str[5][100]
str[0] --> char *
char str[0][100] ---> char [100] ---> char *

int main(int argc,const char *argv[])
{
//char *pstr[5]={"hello","world","qian","feng","welcome"};
char *pstr[5]={NULL};
char str[5][100];
for (int i=0; i<5; i++) {
pstr[i]=str[i];//str[0]~~str[4]都是一维数组
scanf("%s", pstr[i]);
}

for (int i=0; i<5; i++) {
    printf("%s\n", pstr[i]);
}
return 0;

}

int main(int argc,const char *argv[])
{
int a[3][2];
int p = a[0];
//int (
p)[2]=(&a[0]);
printf("a[0]=%p\n", a[0]);// int [2] ---> int *
printf("a[1]=%p\n", a[1]);
printf("a[2]=%p\n", a[2]);

printf("p1 =%p\n", p);
p++;
printf("p2 =%p\n", p);
p++;
printf("p3 =%p\n", p);



return 0;

}

利用指针数组求二维数组元素的和

int sumArr(int *p[], int len1, int len2)//len1表示指针数组元素个数, len2表示指针数组元素指向的一维数组的元素个数
{
int sum=0;
for (int i=0; i<len1; i++) {
for (int j=0; j<len2; j++) {
//sum += p[i][j];
sum += *(p[i]+j);
//sum+= ((p+i)+j);
}
}
return sum;
}

int main(int argc,const char *argv[])
{
int a[3][2]={1,2,3,4,5,6};
int *p[3]={NULL};
for (int i=0; i<3; i++) {
p[i]=a[i];
}
printf("sum = %d\n", sumArr(p,3,2));

return 0;

}

数组的指针 数组指针 , 本质是一个指针 数据类型是数组数据类型
int (p)[3] int ()[3]

int a[5][3]
a --> int [5][3] --> int ()[3]
&a[0] int a[0][3] --> int (
(&a[0]))[3] --> int ()[3]
&a int (
&a)[5][3] --> int (*)[5][3]
&a[0][0] int *

int main(int argc, const char argv[])
{
int (
p)[3]=NULL;
int a[5][3];

p = a;//int (*)[3]

printf("%p\n", &a[0]);
printf("%p\n", &a[0][0]);
printf("%p\n", &a);
printf("%p\n", a);

printf("%p\n", p++);
printf("%p\n", p++);
printf("%p\n", p);


printf("%ld\n", sizeof(int (*)[3]));//int (*)[3]  int (*)[2]
return 0;

}

用一维数组的指针求二维数组元素的和

if 0

int sumArr(int (*p)[3], int len)
{
int sum=0;
for (int i=0; i<len; i++) {
for (int j=0; j<3; j++) {
//sum+= ((p+i)+j);//p++跳过sizeof(int [3]) (p+i) int * sizeof(int)
//sum+= (
(p+i))[j];
sum += p[i][j];
}
//printf("sum = %d\n", sum);
}
return sum;
}

int main(int argc,const char *argv[])
{
int a[2][3]={1,2,3,4,5,6};
printf("sum = %d\n", sumArr(a, 2));

return 0;

}

endif

int sumArr(int (p)[3][2])//p -->int ()[2] **p-- > int *
{
int sum=0;
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
//sum+=(
p)[i][j];
sum += ((p+i))[j];
//sum += ((*p+i)+j);
}
}
return sum;
}

int main(int argc, const char *argv[])
{
int a[3][2]={1,2,3,4,5,6};

printf("sum = %d\n", sumArr(&a));

return 0;

}

函数的指针
int (*pfunc)(int, int) //保存地址 函数的入口地址

int add(int a, int b) //add --> int (*)(int , int);
{
//游戏算法
return a+b;
}

int sub(int a, int b)
{
//视频算法
return a-b;
}

//.lib文件 二进制文件
//
int main(int argc,const char argv[])
{
int (
pfunc)(int, int);
pfunc = add;

printf("%p\n", add);
printf("%ld\n", sizeof(int (int ,int)));
printf("add = %d\n", add(3, 5));
//pfunc++;
//printf("%p\n", pfunc);

printf("add = %d\n", pfunc(3,5));
pfunc = sub;
printf("sub = %d\n", pfunc(10,5));

return 0;

}

//dog
//1.void eat(void) 2.void drink(void) 3.void run(void) 4.void jump(void)

//"eat drink run jump"

if 0

void eat(void)
{
printf("dog is eating\n");
}

void drink(void)
{
printf("dog is drinking\n");
}

void run(void)
{
printf("dog is running\n");
}

void jump(void)
{
printf("dog is jumping\n");
}

include <string.h>

int main(int argc,const char argv[])
{
char order[50]={};
scanf("%s", order);
void (
porder)(void)=NULL;
char action[4]={"eat","drink","run","jump"};
void (
index[4])(void) ={eat, drink,run,jump};

for (int i=0; i<4; i++) {
    if (strcmp(order, action[i])==0) {
        porder =index[i];
        //porder();
        index[i]();
    }
}
return 0;

}

endif

回调函数

void printHello(int value)
{
printf("hello --- %d\n", value);
}

void printWorld(int value)
{
printf("world --- %d\n", value);
}

void printWelcome(int value)
{
printf("welcome --- %d\n", value);
}

void print(void (*pfunc)(int), int cnt, int value)
{
for (int i=0; i<cnt; i++) {
pfunc(value);
}
}

int main(int argc,const char *argv[])
{
print(printHello, 5,1);
print(printWelcome, 5,2);
print(printWorld, 5, 3);

return 0;

}

二级指针

int main(int argc,const char *argv[])
{
int a=10;
int *p = &a;
int **q = &p;

printf("a = %d\n", *p);
printf("%p\n", *q);//*q == &a
printf("%p\n", &a);

printf("%p\n", &p);
printf("%p\n", q);

printf("%d\n", **q);
printf("%d\n", *p);

return 0;

}

利用二级指针交换两个变量的值

void swap(int **a, int **b)
{
int temp = **a;
**a = **b;
**b = temp;
}

int main(int argc,const char *argv[])
{
int a=10,b =20;
int *pa = &a, *pb = &b;
swap(&pa, &pb);
printf("a = %d b = %d\n", a, b);
return 0;
}

yuirtyui@ dfghj@ ertyui dfghjk@
yuirtyui dfghj ertyui dfghjk

include <stdlib.h>

include <string.h>

int main(int argc,const char *argv[])
{
char *pstr[100]={NULL};
int cnt=0;
for (int i=0; i<100; i++) {
pstr[i]= (char )malloc(100sizeof(char));
if (!pstr[i]) {
return -1;
}
scanf("%s", pstr[i]);
cnt++;
int len = (int)strlen(pstr[i]);
if (pstr[i][len-1]=='@') {
pstr[i][len-1]='\0';
//break;
}
if (getchar() =='\n') {
break;
}
}

for (int i=0; i<cnt; i++) {
    printf("%s\n", pstr[i]);
}
//printf("\n");

return 0;

}

int main(int argc,const char *argv[])
{
char str[100]={};
//scanf("%[^\n]", str);//只有'\n'字符不能作为字符串的元素
gets(str);

//scanf("%[A-Z,a-z,0-9]",str);

printf("%s\n",str);
return 0;

}

泛型算法实现
实现一个函数,能够排序int 类型数组 long数组 char类型数组 float类型数组

include <math.h>

int compareInt(void *a, void *b)
{
return *(int )a -(int *)b;
}

int compareLong(void *a, void b)
{
long val =
(long *)a - *(long *)b;
if (val==0) {
return 0 ;
}
else if(val < 0)
{
return -1;
}
else
{
return 1;
}
}

int compareFloat(void *a, void *b)
{
float f = *(float )a -(float *)b;
if (fabs(f) <0.00005) {
return 0;
}
else if(f<0)
{
return -1;
}
else
{
return 1;
}
}

int compareChar(void *a, void *b)
{
return *(char *)a - *(char *)b;
}

void sortArr(int (*compare)(void *, void *), int size, int len)
{

//memcpy

}

int main(int argc,const char *argv[])
{

return 0;

}

posted on 2014-01-30 20:30  Sinner_Yun  阅读(241)  评论(0编辑  收藏  举报

导航