用 C 语言解释算术指针操作?


指针是一种变量,它存储着另一个变量的地址。

指针声明、初始化以及获取

考虑以下语句 −

int qty = 179;

声明指针

int *p;

‘p’是一个指针变量,它保存着另一个整型变量的地址。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

指针初始化

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

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

使用指针的算数运算

指针变量可以在表达式中使用。例如,如果指针变量正确声明和初始化,那么以下语句是有效的。

a) *p1 + *p2
b) *p1- *p2
c) *p1 * *p2
d) *p1/ *p2 Note: There must be a blank space between / and *
otherwise it is treated as beginning of comment line
e ) p1 + 4
f) p2 - 2
g) p1 - p2 Note: returns the no. of elements in between p1 and p2 if
both of them point to same array
h) p1++
i) – – p2
j) sum + = *p2
j) p1 > p2
k) p1 = = p2
l) p1 ! = p2 Note: Comparisons can be used meaningfully in handling
arrays and strings

以下语句无效 −

a) p1 + p2
b) p1 * p2
c) p1 / p2
d) p1 / 3

程序

#include<stdio.h>
main (){
   int a,b,x,y,z;
   int *p1, *p2;
   a =12;
   b = 4;
   p1= &a;
   p2 = &b;
   x = *p1 * * p2  6;
   y= 4 - *p2 / *p1+10;
   printf (“Address of a = %d”, p1);
   printf (“Address of b = %d”, p2);
   printf (“a= %d b =%d”, a,b);
   printf (“x= %d y =%d”, x,y);
}

输出

Address of a = 1234
Address of b = 5678
a = 12 b= 4
x = 42 y= 14

解释

更新于: 09-Mar-2021

1K+ 视图

开启您的 职业

完成该课程以获取认证

开始
广告