解釋 C 程式設計中的引用和指標?


問題

用示例解釋 c 程式語言中引用和指標的概念。

引用

  • 這是我們宣告的變數的別名。

  • 可以透過按值傳遞來訪問它。

  • 它不能儲存空值。

語法

datatype *variablename

例如,int *a; //a 包含 int 型別變數的地址。

指標

  • 它儲存變數的地址。

  • 我們可以使用指標儲存空值。

  • 可以透過按引用傳遞來訪問它。

  • 宣告變數時不需要初始化。

語法

pointer variable= & another variable;

示例

 Live Demo

#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

更新於:09-Mar-2021

777 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.