编写一个C程序,演示指针的示例

指针是一个存储另一个变量地址的变量。

指针的特点

指针节省内存空间。

由于直接访问内存位置,指针的执行时间更快。

在指针的帮助下,内存被有效地访问,即动态分配和释放内存。

指针与数据结构一起使用。

声明一个指针

int *p;

这意味着“p”是一个指针变量,它保存另一个整型变量的地址。

指针的初始化

地址运算符(&)用于初始化指针变量。

例如,

int qty = 175;int *p;p= &qty;

编写一个C程序,演示指针的示例

通过变量访问变量指针

要访问变量的值,请使用间接运算符 (*)。

程序

 现场演示

#includevoid 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程序,演示指针的示例的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1445568.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 22:38:22
下一篇 2025年12月13日 17:46:54

发表回复

登录后才能评论
关注微信