指針是一個(gè)存儲(chǔ)另一個(gè)變量地址的變量。
指針的特點(diǎn)
指針節(jié)省內(nèi)存空間。
由于直接訪問(wèn)內(nèi)存位置,指針的執(zhí)行時(shí)間更快。
在指針的幫助下,內(nèi)存被有效地訪問(wèn),即動(dòng)態(tài)分配和釋放內(nèi)存。
指針與數(shù)據(jù)結(jié)構(gòu)一起使用。
聲明一個(gè)指針
int *p;
登錄后復(fù)制
這意味著“p”是一個(gè)指針變量,它保存另一個(gè)整型變量的地址。
指針的初始化
地址運(yùn)算符(&)用于初始化指針變量。
例如,
int qty = 175; int *p; p= &qty;
登錄后復(fù)制
通過(guò)變量訪問(wèn)變量指針
要訪問(wèn)變量的值,請(qǐng)使用間接運(yùn)算符 (*)。
程序
?現(xiàn)場(chǎng)演示
#include<stdio.h> void main(){ //Declaring variables and pointer// int a=2; int *p; //Declaring relation between variable and pointer// p=&a; //Printing required example statements// printf("Size of the integer is %d",sizeof (int));//4// printf("Address of %d is %d
",a,p);//Address value// printf("Value of %d is %d
",a,*p);//2// printf("Value of next address location of %d is %d
",a,*(p+1));//Garbage value from (p+1) address// printf("Address of next address location of %d is %d
",a,(p+1));//Address value +4// //Typecasting the pointer// //Initializing and declaring character data type// //a=2 = 00000000 00000000 00000000 00000010// char *p0; p0=(char*)p; //Printing required statements// printf("Size of the character is %d
",sizeof(char));//1// printf("Address of %d is %d
",a,p0);//Address Value(p)// printf("Value of %d is %d
",a,*p0);//First byte of value a - 2// printf("Value of next address location of %d is %d
",a,*(p0+1));//Second byte of value a - 0// printf("Address of next address location of %d is %d
",a,(p0+1));//Address value(p)+1// }
登錄后復(fù)制
輸出
Size of the integer is 4 Address of 2 is 6422028 Value of 2 is 2 Value of next address location of 2 is 10818512 Address of next address location of 2 is 6422032 Size of the character is 1 Address of 2 is 6422028 Value of 2 is 2 Value of next address location of 2 is 0 Address of next address location of 2 is 6422029
登錄后復(fù)制
以上就是編寫一個(gè)C程序,演示指針的示例的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.xfxf.net其它相關(guān)文章!