我有下面的代码,它引发了一个分段错误.请建议可以做些什么.
#include <stdio.h>
int main() {
char *p ,*q ;
p =(char *)malloc(20) ;
*p = 30 ;
p++ ;
p=q ;
free(q) ;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
考虑以下两个程序:
/***************correct: no error for this code **************/
#include <stdio.h>
#include <string.h>
int main()
{
char *p ,*q ;
p =(char *)malloc(10) ;
strcpy( p , "AB") ;
*p = '\0' ;
p++ ;
q = p ;
//*q = 32 ;
free(q) ;
return 0;
}
/*************code2 which gives error ********************/
#include <stdio.h>
#include <string.h>
int main()
{
int *p ,*q ;
p =(int *)malloc(10) ;
*p = 30 ;
p++ ;
q = p ;
*q = 32 …Run Code Online (Sandbox Code Playgroud)