2
這裏是我的代碼,它在這裏出錯strcpy(pSrcString,"muppet");
事實上,它每當我使用strcpy。是什麼導致這個strcpy段錯誤?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *pSrcString = NULL;
char *pDstString = NULL;
/* muppet == 6, so +1 for '\0' */
if ((pSrcString = malloc(7) == NULL))
{
printf("pSrcString malloc error\n");
return EXIT_FAILURE;
}
if ((pDstString = malloc(7) == NULL))
{
printf("pDstString malloc error\n");
return EXIT_FAILURE;
}
strcpy(pSrcString,"muppet");
strcpy(pDstString,pSrcString);
printf("pSrcString= %s\n",pSrcString);
printf("pDstString = %s\n",pDstString);
free(pSrcString);
free(pDstString);
return EXIT_SUCCESS;
}