考虑以下两个程序:
/***************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 ;
free(q) ;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你能解释为什么第一个有效,但第二个没有?
| 归档时间: |
|
| 查看次数: |
372 次 |
| 最近记录: |