【问题标题】:I need help to understand the syntax of pointers [duplicate]我需要帮助来理解指针的语法[重复]
【发布时间】:2019-05-18 01:35:18
【问题描述】:

我是指针的新手,我对这部分代码感到困惑。我有几个问题:

  1. 在第 1 行,我不确定双星号是什么意思。我读到它是另一个指针的指针,但我不知道那是什么。

  2. 在第 4 行,我不知道如何解释语法:

    *(*(total_number_of_pages + x) + y)
    
/* total_number_of_pages

 * This stores the total number of pages in each book of each shelf.
 * The rows represent the shelves and the columns represent the books.
*/

int** total_number_of_pages; // (Line 1)    
int x, y; // (Line 2)    
scanf("%d %d", &x, &y); // (Line 3)    
printf("%d\n", *(*(total_number_of_pages + x) + y)); // (Line 4)

【问题讨论】:

    标签: c pointers


    【解决方案1】:

    确实*(*(total_number_of_pages + x) + y) 很难“理解”。这就是 C 语言提供另一种表示法的原因之一:*(E1 + E2) 也可以写成E1[E2](或E2[E1],因为+ commutes)。我们可以在您的表达式中应用此规则以获得:*(total_number_of_pages[x] + y),然后再次:total_number_of_pages[x][y]

    这更清楚。 total_number_of_pages 是指向类似数组的内存区域基址的指针。 total_number_of_pages[x] 表示 x-th 元素的值。该元素本身是指向另一个数组的指针,其中[y] 表示y-th 值。

    在您的程序中,total_number_of_pages 没有被赋予任何值,这意味着它在表达式 *(*(total_number_of_pages + x) + y) 中的使用会调用未定义的行为。要使表达式有效,它必须指向至少具有x + 1 元素的指针数组,并且该数组的[x] 元素必须指向至少具有int 元素的int 数组.

    【讨论】:

      【解决方案2】:

      想象一下,如果您有一排书,并且您想知道该行中每本书有多少页。您可以使用存储在连续内存地址中的整数集合。

      现在假设您有很多行书籍。您可以使用指向每一行书籍的指针集合,每行书籍是存储在连续内存地址中的整数集合。

      由于每一行都是一个指针,所以行的地址是一个指向指针的指针。因此int**

      现在,total_number_of_pages 是指向行的第一个指针的指针。要获得第二个指针,您需要向它添加一个。要到达x'th 行,您需要在其中添加x。如果取消引用该指针,您将获得指向x'th 行中第一本书的指针。如果您想要y'th 书,请将y 添加到其中。现在,您在第 x'th 行中有一个指向第 y'th 书的指针,您可以取消引用以获取该书的页数。

      如果您对指针不是很熟悉,那么您真的不应该尝试理解这样的代码。

      【讨论】:

        【解决方案3】:

        指针是包含内存地址的变量。要声明指针变量,只需在名称前使用星号声明常规变量即可。

        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 = &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)
        

        【讨论】:

        • c中没有iostream
        猜你喜欢
        • 1970-01-01
        • 2011-05-04
        • 2016-01-11
        • 1970-01-01
        • 2016-12-14
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        • 2020-05-03
        相关资源
        最近更新 更多