谁能解释一下,为什么VC++会报错?

时间:2022-10-28 14:52:43

我用的VC++ 6.0,用C写一个约会表的小程序,在Win-TC下,可以编译成功,但在VC++下,就报错了:

报错信息:第39行:enter函数中p=malloc(strlen(s));                    

                   error C2440: '=' : cannot convert from ‘void *' to 'char *'                    

                  Conversion from 'void*' to pointer to non-'void' requires an explicit cast

源程序:#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#define MAX 100

char *p[MAX],*qretrieve();
int spos;
int rpos;
void enter(),qstore(char *),review(),qdelete();
void main()
{
    char s[80];
    int t;

    for(t=0;t<MAX;++t) p[t]=NULL; /*init array to nulls*/
    spos=0;rpos=0;

    for(;;){
        printf("Enter,List,Remove,Quit: ");
        gets(s);
        *s=toupper(*s);
        switch(*s){
        case 'E':enter();break;
        case 'L':review();break;
        case 'R':qdelete();break;
        case 'Q':exit(0);
        }
    }
}

/*Enter Appointments In Queue*/
void enter()
{
    char s[256],*p;
    do{
        printf("enter appointment %d: ",spos+1);
        gets(s);
		if(*s==0)break;
        p=malloc(strlen(s));
        if (!p){
             printf("out of memory/n");
             return;
        }
        strcpy(p,s);
        if (*s) qstore(p);
    }while(*s);
}

void review()
{
    int t;
    for(t=rpos;t<spos;++t)
        printf("%d. %s/n",t+1,p[t]);
}

void qdelete()
{
    char *p;
    if(!(p=qretrieve())) return;
    printf("%s/n",p);
}

/*store an appointment*/
void qstore(char *q)
{
    if(spos==MAX){
        printf("List full/n");
        return;
    }
    p[spos]=q;
    spos++;
}

/*retrieve an appointment*/
char *qretrieve()
{
    if (rpos==spos){
        printf("No (more)appointment/n");
        return NULL;
    }
    rpos++;
    return p[rpos-1];
}