原文: https://beginnersbook.com/2019/02/c-program-to-create-initialize-and-access-a-pointer-variable/

在本教程中,我们将编写一个 C 程序来创建,初始化和访问指针变量。要学习指针的基础知识,请参阅我的教程: C 指针

示例:用于创建,访问和初始化指针的程序

在下面的程序中,我们声明了一个字符变量ch和字符指针pCh,之后我们用 char ch的地址值初始化了指针变量pCh

该示例还说明了如何使用指针变量pCh访问ch的值和地址。

  1. /* Created by Chaitanya for Beginnersbook.com
  2. * C program to create, initialize and access a pointer
  3. */
  4. #include <stdio.h>
  5. int main()
  6. {
  7. //char variable
  8. char ch;
  9. //char pointer
  10. char *pCh;
  11. /* Initializing pointer variable with the
  12. * address of variable ch
  13. */
  14. pCh = &ch;
  15. //Assigning value to the variable ch
  16. ch = 'A';
  17. //access value and address of ch using variable ch
  18. printf("Value of ch: %c\n",ch);
  19. printf("Address of ch: %p\n",&ch);
  20. //access value and address of ch using pointer variable pCh
  21. printf("Value of ch: %c\n",*pCh);
  22. printf("Address of ch: %p",pCh);
  23. return 0;
  24. }

输出:

C 程序:创建,初始化和访问指针变量 - 图1

相关 C 示例

  1. C 程序:检查闰年
  2. C 程序:交换两个数字
  3. C 程序:查找intfloatdoublechar的大小
  4. C 程序:查找字符的 ASCII 值