您好,欢迎访问一九零五行业门户网

编写一个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 4address of 2 is 6422028value of 2 is 2value of next address location of 2 is 10818512address of next address location of 2 is 6422032size of the character is 1address of 2 is 6422028value of 2 is 2value of next address location of 2 is 0address of next address location of 2 is 6422029
以上就是编写一个c程序,演示指针的示例的详细内容。
其它类似信息

推荐信息