指针的传参数与赋值问题
2014-03-10 21:13
204 查看
1.指针的赋值
eg:int (a[2]) [3];
则 int *p1 = &a[0][0];
int (*p2) [3] = &a[0];
int (*p3)[2][3] = &a;
2.指针的传参问题(1)
void get_m(int *p,int num)
{
p=(int*)malloc(sizeof(int)*num)
}
void test(void)
{
int *str=NULL;
get_m(str,100);
strcpy(str,"hello");
}
点评:对于该程序,一开始str指向空,然后把str的地址赋予p=00000000,然后p又malloc一块空间,假设p=12345678,然后就没了,值传递改变不了str的值,str的值仍然为空;
3.指针的传参问题(2)
void get_m(int **p,int num)
{
*p=(int*)malloc(sizeof(int)*num)
}
void test(void)
{
int *str=NULL;
get_m(str,100);
strcpy(str,"hello");
}
点评:对于该程序,一开始str指向空,然后把str的地址传给**p,既p指向str,*p=str=malloc一块空间,所以str指向malloc开的空间;
4.栈空间的释放问题
char *get_(void)
{
char p[]="hello world";
return p;
}
void test(void)
{
char *str=NULL;
str = get_();
printf("%s",str);
}
点评:一开始str指向空,p数组对于数据段的hello world,因为p是在栈中所以引用完后p被释放;所以结果不确定,不能再引用;
5.
eg:int (a[2]) [3];
则 int *p1 = &a[0][0];
int (*p2) [3] = &a[0];
int (*p3)[2][3] = &a;
2.指针的传参问题(1)
void get_m(int *p,int num)
{
p=(int*)malloc(sizeof(int)*num)
}
void test(void)
{
int *str=NULL;
get_m(str,100);
strcpy(str,"hello");
}
点评:对于该程序,一开始str指向空,然后把str的地址赋予p=00000000,然后p又malloc一块空间,假设p=12345678,然后就没了,值传递改变不了str的值,str的值仍然为空;
3.指针的传参问题(2)
void get_m(int **p,int num)
{
*p=(int*)malloc(sizeof(int)*num)
}
void test(void)
{
int *str=NULL;
get_m(str,100);
strcpy(str,"hello");
}
点评:对于该程序,一开始str指向空,然后把str的地址传给**p,既p指向str,*p=str=malloc一块空间,所以str指向malloc开的空间;
4.栈空间的释放问题
char *get_(void)
{
char p[]="hello world";
return p;
}
void test(void)
{
char *str=NULL;
str = get_();
printf("%s",str);
}
点评:一开始str指向空,p数组对于数据段的hello world,因为p是在栈中所以引用完后p被释放;所以结果不确定,不能再引用;
5.
相关文章推荐
- 带有指针参数的函数内给指针参数赋值时注意事项
- 函数参数的传递问题(一级指针和二级指针)
- 函数参数为指针,内部进行开辟内存问题
- 指针参数传递内存问题
- 指针参数的问题
- 带query参数报表api运算空指针问题解决方案
- C语言函数传递指针参数的问题
- c语言链表 关于指针参数调用问题
- C++指针作为函数的参数进行传递时注意的问题
- 函数参数的传递问题(一级指针和二级指针)
- 需要换行的长字符串赋值给指针需要注意的问题
- 函数参数的传递问题(指针的指针)
- pthread_create 参数传递指针问题
- 函数传递指针参数时注意的问题
- 关于const常量与指针赋值的问题
- 关于数组和指针作为参数时遇到的问题
- 函数参数的传递问题(一级指针和二级指针)
- 函数参数的传递问题(指针的指针)
- 函数参数的传递问题(一级指针和二级指针)
- 函数的结构体指针参数传递的问题 都忘完了