指针是包含内存地址的变量。要声明指针变量,只需在名称前使用星号声明常规变量即可。
int *ptr;
我将尝试用一些图形和更少的代码向您解释指针。
#include <iostream>
int main()
{
int num = 5; //Just a regular integer variable.
char hello[] = "Hello World!"; // An array of characters.
int* point_to_num = # // & return's the memory address of a variable.
char* point_to_hello = hello; //Notice that I don't use &. Arrays are already pointers!
//----------Dereferencing Pointers----------//
printf("%d\n", *point_to_num); //This will print 5
//No Dereferencing needed.
printf("%s\n", hello); //This will print "Hello World!"
printf("%s\n", point_to_hello); //This will print "Hello World!" again!
return 0;
}
如果我们将代码与图像进行比较,point_to_num 是包含内存地址 3000 的 第三个 矩形。当您使用星号取消引用指针时:
printf("%d\n", *point_to_num); //This will print 5
你是说“把内存地址 3000 中包含的值带给我”
但是在这种情况下:
printf("%s\n", hello); //This will print "Hello World!"
printf("%s\n", point_to_hello); //This will print "Hello World!" again!
字符串是一个字符序列,您必须提供一个指向该序列开头的指针,以便 printf 打印字符串,直到找到特殊的 null terminate character。
在你的代码中
int** total_number_of_pages;
total_number_of_pages 未初始化,这意味着我们无法说出输出将是什么。
printf("%d\n", *(*(total_number_of_pages + x) + y)); // (Line 4)