How to print the content pointed to by a pointer in C language?

To print the content pointed to by a pointer, first use the dereference operator * to fetch the value being pointed to, and then use the printf function to print out that value. Here is a simple example code:

#include <stdio.h>

int main() {
    int num = 10;
    int *ptr = &num; // 指针指向num变量

    printf("指针指向的内容为: %d\n", *ptr); // 打印指针指向的内容

    return 0;
}

In the example above, an integer variable num and an integer pointer ptr are first defined. The pointer is then pointed to the num variable. Finally, *ptr is used to retrieve the value pointed to by the pointer, which is then printed out using the printf function. Running the program will output the content pointed to by the pointer as 10.

bannerAds