#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void main1() //報錯
{
char *p1=NULL;//指針賦值爲NULL,即爲0
//p1=0x77;
strcpy(p1,"asdfg");
system("pause");
return;
}
void main()
{
inti,j=0;
char buf[128];//C語言可以在棧上分配內存
char*p1 = NULL;
char*p2 = NULL;
p1 =&buf[0]; //不斷地修改p1的值,相當於不斷改變指針的指向
p1= &buf[1];
p1= &buf[2];
for(i=0; i<10 ;i++ )
{
p1= &buf[i];
}
p2 = (char *)malloc(100); //在堆中申請內存
strcpy(p2,"abcdefg123456789");
for(i=0; i<10 ;i++ )
{
p1= p2+i;
printf("%c ",*p1); //不同的指針可以操作同一塊內存空間
}
system("pause");
return;
}