1. 程式人生 > >向NULL地址COPY資料和不斷改變指標指向

向NULL地址COPY資料和不斷改變指標指向

#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;

}