指針是一個存儲另一個變量地址的變量。
指針的特點
指針節(jié)省內存空間。
由于直接訪問內存位置,指針的執(zhí)行時間更快。
在指針的幫助下,內存被有效地訪問,即動態(tài)分配和釋放內存。
指針與數(shù)據(jù)結構一起使用。
聲明一個指針
int *p;
登錄后復制
這意味著“p”是一個指針變量,它保存另一個整型變量的地址。
指針的初始化
地址運算符(&)用于初始化指針變量。
例如,
int qty = 175; int *p; p= &qty;
登錄后復制
通過變量訪問變量指針
要訪問變量的值,請使用間接運算符 (*)。
程序
?現(xiàn)場演示
#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// }
登錄后復制
輸出
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
登錄后復制
以上就是編寫一個C程序,演示指針的示例的詳細內容,更多請關注www.xfxf.net其它相關文章!