Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of pointer Operation in C language

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "C language pointer operation example analysis". In daily operation, I believe that many people have doubts about C language pointer operation example analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "C language pointer operation example analysis"! Next, please follow the editor to study!

Pointer operation pointer ±integer

-pointer.-pointer.

Relational operation of pointer

4.1pointer ±integer # define VALUE 5int main () {float values [VALUE]; float * vp; / / pointer +-pointer, relational operation for (vp = & values [0]; vp

< &values[VALUE];) { *vp++ = 0;//通过这样完成初始化 } for (int i = 0; i < VALUE; i++) { printf("%d ", values[i]); } return 0;} 4.2 指针-指针int main(){ int arr[10] = { 0 }; printf("%d\n", &arr[9] - &arr[0]);//得到元素的个数 printf("%d\n", sizeof(arr)/sizeof(arr[0]));//计算元素个数 int* p0 = &arr[0]; int* p9 = &arr[9]; printf("%d\n", (p9 - p0));//得到元素的个数 return 0;} 通过数组首尾元素的地址相减,得出的就是数组元素的个数, -int*表示指针指向的地址里面,存放的数据类型是整形,占用4个字节 数组元素地址+1,就是寻找下一个元素的地址,就会移动4个字节 通过调试更加清楚的观察到这一现象,int 数组每个元素占用四个字节: &arr[0]: 0x005cf7f8 &arr[0]+1: 0x005cf7fc,地址移动1位,即int*指针移动1位,字节增加4个。 前提条件:两个指针必须指向同一块空间: char ch[5];int arr[6];%arr[4]-&ch[3];//错误的 前面的文章介绍了字符串长度的方法两种方法: 循环 递归 这里介绍第三种方法, 指针 int mylen(char* pa)//传参是数组首元素的地址{ char* p = pa;//首元素地址 while (*pa)//元素不是'0' { pa++;//通过地址访问字符串 } //结尾字符0的地址减首元素地址 return pa - p;//指针相减是元素的个数}int main(){ char arr[] = "abcdef"; int len = mylen(arr); printf("%d", len); return 0;}4.3 指针的关系运算 标准规定:允许指向数组元素的指针与指向数组最后一个元素后面的那个内存位置的指针比较,但是不允许与 指向第一个元素之前的那个内存位置的指针进行比较 int main(){ // for (vp = &values[5]; vp >

& values [0];) {*-- vp = 0;} for (vp = & values [5-1]; vp > = & values [0]; vp--) / / address is not allowed to point to the address in front of the first element, which is not allowed {* vp = 0;} return 0 } 5. Pointer and array int main () {int arr [10] = {1, int, 2, 5, 5, 6, 8, 9, 0}; printf ("% p\ n", arr); printf ("% p\ n", & arr [0]); int * p = arr;//p stores the address return 0 of the first element of the array;}

The array name is the same as the address of the first element of the array. The array name represents the address of the first element of the array. (except for 2 cases, which are described in detail in the array series)

Since the array name can be stored in a pointer as an address, it is possible for us to use the pointer to access one

Int main () {int arr [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; int * p = arr; / / the address where the pointer stores the first element of the array int sz = sizeof (arr) / sizeof (arr [0]); for (int item0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report