读书人

c语言的指针有关问题,在这好纠结

发布时间: 2012-04-13 13:50:24 作者: rapoo

c语言的指针问题,在这好纠结,在线等。。。。
#include<stdio.h>
void test(int *p)
{
int *q;
q=(int *)malloc(sizeof(int));
*q=10;
p=q;
printf("%d\n",*p); //程序执行到此出指针q已经赋值,能打印出来数值10,问题在主函数
}
int main(void)
{
int *st;
st=NULL;
test(st);
printf("%d\n",*st); //跳出test(st)函数后,st指针怎么又变为空了呢???打印不出来。。。。
return 0;

}


问题在主函数的注释

求解! 感谢!

[解决办法]
形参是不能改变他的值得。。。
[解决办法]
上一楼贴错代码了...

C/C++ code
#include<stdio.h>#include <stdlib.h>void test(int **p){  int *q;  q=(int *)malloc(sizeof(int));  *q=10;  *p=q;  printf("%d\n",**p);}int main(void){  int *st;  st=NULL;  test(&st);  printf("%d\n",*st);  return 0;  } 

读书人网 >C语言

热点推荐