Linux/C 高级——指针函数
1.概念
本质是函数,函数的返回值为指针。类比着指针数组。指针数组:本质是数组,数组中存放指针。
数据类型 *数组名[元素个数];
int a[2][3];
int *arr[2] ={a[0],a[1]};
//*(*(arr+i)+j) *(arr[i]+j) arr[i][j]
2.定义格式
格式:数据类型 *函数名(参数列表)
{
函数体;
return 地址; //当失败时一般返回NULL
}
3.函数内开辟空间
案例一
#include <stdio.h>char *fun()
{char buf[32] = "hello";return buf;
}int main(int argc, char const *argv[])
{char *p = fun();printf("%s\n", p);return 0;
}
结果未知,因为返回局部变量地址,函数调用结束后没有权限使用栈区的空间了所以结果是不可控的。如果被其他进程占用,可能会报段错误或者打印奇怪的东西。
修改:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>char *fun()
{char *p = (char *)malloc(32);if (NULL == p){printf("malloc lost\n");return NULL;}// p="hello"; //让p又指向了常量区的hello而不指向堆区了strcpy(p, "hello");return p;
}int main(int argc, char const *argv[])
{char *p = fun(); if (NULL == p){printf("fun err\n"); //如果函数返回值为NULL可以打印一句错误提示return -1; //如果错误让主函数结束,程序不再往后运行了}printf("%s\n", p);free(p);p = NULL;return 0;
}
案例二
#include <stdio.h>
#include <stdlib.h>
#include <string.h>void fun(char *p) //p=NULL
{p = (char *)malloc(32);if (NULL == p){printf("malloc lost\n");}strcpy(p, "hello");
}int main(int argc, char const *argv[])
{char *p = NULL;fun(p);printf("%s\n", p); //函数调用结束后,主函数中的p还是指向了NULL,打印所以段错误free(p);p = NULL;return 0;
}
报端错误:因为主函数中p指向了NULL,函数内改变形参p不会影响到主函数中的变量p, 所以函数调用完后主函数中的p还是指向了NULL。
修改:传递二级指针
#include <stdio.h>
#include <stdlib.h>
#include <string.h>void fun(char **p) //p = &q;
{*p = (char *)malloc(32); //*p = *&q = qif (NULL == *p){printf("malloc lost\n");}strcpy(*p, "hello"); //*p = *&q = q
}int main(int argc, char const *argv[])
{char *q = NULL;fun(&q); //函数调用结束后,q指向了堆区空间printf("%s\n", q);free(q);q = NULL;return 0;
}
案例三:数组传递
#include <stdio.h>
#include <stdlib.h>
#include <string.h>void fun(int arr[5]) //arr类型为int *, 本质还是传递指针
{arr[0] = 100;printf("%ld\n", sizeof(arr));
}int main(int argc, char const *argv[])
{int a[5] = {1, 2, 3, 4, 5};fun(a);printf("%d\n", a[0]);return 0;
}
传递数组的形式是为了让用户知道要传递的是一个数组,但本质还是传递数组的首地址,也就是传递指针。