注册 登录
编程论坛 C++教室

大虾们看看这程序出错在那啊!!!

fanfan320 发布于 2010-05-24 14:57, 631 次点击
#include"stdio.h"
#include"iostream.h"
#include"string.h"
void getmemory(char **p)
{
 *p=(char*)malloc(100);
}
void main()
{
char *str=NULL;
getmemory(&str);
strcpy(str,"hello word");
printf(str);
}

[ 本帖最后由 fanfan320 于 2010-5-24 15:15 编辑 ]
6 回复
#2
apull2010-05-24 18:25
#include"iostream.h"去掉了试试看。。
#3
kekin2010-05-24 18:59
没有错呀  我觉得是软件问题不知道楼主用的是什么软件  我用VC++6.0已经能运行了   
楼主能不能把出错提示贴上来   
还有救楼主的程序格式很不好呀  或许会导致出错  向下面这样就好了
#include"stdio.h"
#include"iostream.h"
#include"string.h"
void getmemory(char **p)
{
    *p = (char*)malloc(100);
}
void main()
{
    char *str = NULL;
    getmemory(&str);
    strcpy(str, "hello word");
    printf(str);
    printf("\n");
}
#4
rwyangguang2010-05-24 19:33
#include"stdio.h"
#include"iostream.h"
#include"string.h"
#include"stdlib.h"//malloc要加这个头文件
void getmemory(char **p)
{
*p=(char*)malloc(100);
}
void main()
{
char *str=NULL;
getmemory(&str);
strcpy(str,"hello word");
printf(str);
}
#5
最近不在2010-05-24 22:44
程序代码:
// Note:Your choice is C++ IDE
#include <iostream>
using namespace std;

void GetMem(char **p, int n)   //第一个参数为指针的指针(地址),第二个参数为分配内存大小
{
    *p = new char[n];
}

int main()
{
    char *a = "abcdef";
    int n = strlen(a);
    char *p = 0;
   
    GetMem(&p, (n+1));   //动态内存的意义在于动态分配够用的空间。这样才能体现动态分配的意义。
    strcpy(p, a);   //拷贝字符串
    cout<<p<<endl;
   
    delete [] p;  //记得回收内存...
   
    return 0;
}
#6
fanfan3202010-05-25 11:12
回复 3楼 kekin
我也用的是C++6.0,总出现那malloc 没定义,加了个stdlib.h头文件就好了
#7
fanfan3202010-05-25 11:24
回复 5楼 最近不在
呵呵,谢谢了啊。学了不少啊,
1