編寫一個展示指標示例的 C 程式
指標是指向另一個變數的地址的變數。
指標的特徵
指標可節省記憶體空間。
由於直接訪問記憶體位置,指標執行速度更快。
藉助指標,可以有效地訪問記憶體,即動態地分配和釋放記憶體。
指標與資料結構一起使用。
宣告指標
int *p;
表示“p”是一個指向另一個整數變數地址的指標變數。
指標的初始化
地址運算子 (&) 用於初始化指標變數。
例如,
int qty = 175; int *p; p= &qty;

透過其指標訪問變數
使用間接運算子 (*) 訪問變數的值。
程式
#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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP