首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

程序分析,该怎么处理

2013-11-18 
程序分析程序的设计初衷是想输出hello,在VC下的运行结果确实输出一个NULL,请分析原因?#include stdlib.

程序分析
程序的设计初衷是想输出hello,在VC下的运行结果确实输出一个<NULL>,请分析原因?
#include <stdlib.h>
#include <string.h >
#include <stdio.h>

void GetMemory(char *p,int num)
{
p = (char*)malloc(sizeof(char)*num);
strcpy(p,"hello"); 
}

int main()
{
char *str = NULL;
GetMemory(str,100); 
printf("%s",str); 
    return 0;
}

[解决办法]
哎。。。。。。。。
传值 和 传址 楼主好好研究下吧
#include <stdlib.h>
#include <string.h >
#include <stdio.h>

void GetMemory(char **p,int num)
{
*p = (char*)malloc(sizeof(char)*num);
strcpy(*p,"hello"); 
}

int main()
{
char *str = NULL;
GetMemory(&str,100); 
printf("%s",str); 
    return 0;
}
[解决办法]
你传过去的是指针指向的地址,所以也只是指针指向的地址的值的拷贝而已!所以没有变换,还是空!
应该传指针本身的地址,然后再修改指针指向的值才能达到目的的!


void GetMemory(char **p,int num) //函数原型!
GetMemory(&str,100); //函数调用

热点排行