首页 > 文章列表 > 解释一下C语言中的引用和指针?

解释一下C语言中的引用和指针?

引用 关键词:C语言 指针。
161 2023-09-03

问题

使用示例解释C编程语言中引用和指针的概念。

引用

  • 它是我们声明的变量的替代名称。

  • 可以通过传值方式访问。

  • 它不能保存空值。

语法

datatype *variablename

例如,int *a; //a包含int类型变量的地址。

指针

  • 它存储变量的地址。

  • 我们可以使用指针来保存空值。

  • 可以通过引用传递来访问它。

  • 在声明变量时不需要初始化。

语法

pointer variable= & another variable;

Example

 实例演示

#include<stdio.h>
int main(){
   int a=2,b=4;
   int *p;
   printf("add of a=%d

",&a);    printf("add of b=%d

",&b);    p=&a; // p points to variable a    printf("a value is =%d

",a); // prints a value    printf("*p value is =%d

",*p); //prints a value    printf("p value is =%d

",p); //prints the address of a    p=&b; //p points to variable b    printf("b value is =%d

",b); // prints b value    printf("*p value is =%d

",*p); //prints b value    printf("p value is =%d

",p); //prints add of b }

输出

add of a=-748899512
add of b=-748899508
a value is =2
*p value is =2
p value is =-748899512
b value is =4
*p value is =4
p value is =-748899508