解释C语言中指针访问的概念
指针是一个变量,它存储着其他变量的地址。
指针声明、初始化和访问
考虑以下语句 −
int qty = 179;

声明一个指针
int *p;
“p”是一个指针变量,它保存着另一个整型变量的地址。
指针初始化
地址运算符 (&) 用于初始化指针变量。
int qty = 175; int *p; p= &qty;

让我们考虑一个示例,说明指针在访问字符串数组中的元素时如何有用。
在这个程序中,我们尝试访问处于特定位置的元素。可以通过使用操作找到该位置。
将预增指针添加到预增指针字符串中,然后减去 32,即可获得该位置的值。
示例
#include<stdio.h>
int main(){
char s[] = {'a', 'b', 'c', '
', 'c', '\0'};
char *p, *str, *str1;
p = &s[3];
str = p;
str1 = s;
printf("%d", ++*p + ++*str1-32);
return 0;
}输出
77
说明
p = &s[3]. i.e p = address of '
'; str = p; i.e str = address of p; str1 = s; str1 = address of 'a'; printf ("%d", ++*p + ++*str1 - 32); i.e printf("%d", ++
+ a -32); i.e printf("%d", 12 + 97 -32); i.e printf("%d", 12 + 65); i.e printf("%d", 77); Thus 77 is outputted
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP