如何取消引用二维数组的整数?

时间:2018-01-28 19:35:27

标签: c

async function deleteRows() {
    try {
        await Excel.run(async (context) => {

            const expensesTable = context.workbook.tables.getItem("ExpensesTable")

            const bodyRows = expensesTable.getDataBodyRange().load("rowCount");

            await context.sync();

            if (bodyRows.rowCount == 7) {
                const lastThreeRows = bodyRows.getOffsetRange(4, 0).getResizedRange(-4, 0);
                lastThreeRows.delete(null);
                await context.sync();
            }
        });
    }
    catch (error) {
        OfficeHelpers.UI.notify(error);
        OfficeHelpers.Utilities.log(error);
    }
}

我期待着' 4'打印,但我得到了一个记忆位置。

3 个答案:

答案 0 :(得分:4)

您可以像使用一元*运算符一样取消引用它。

但是你必须注意operator precedence将在这里起作用,因为数组下标运算符的优先级高于取消引用运算符。因此你必须这样做。

(*s.p)[x][y]

此外,您使用错误的格式来打印整数。 "%p"格式是打印实际指针,而不是整数。使用"%d"

作为替代方案,请记住,数组会自然地衰减指向其第一个元素的指针。这意味着对于您的数组aa,使用它就会衰减为&aa[0],类型为int (*)[2]。这意味着你可以使你的变量成为相同的类型,并在初始化中使用普通aa,并像普通的“2d”数组一样使用它:

typedef struct
{
  int (*p)[2];
} S;

int main()
{
  int aa[2][2] = {{1, 2}, {3, 4}};
  S s = { .p = aa };  // Note: Not using the address-of operator here
  printf("Four: %d\n", s.p[1][1]);  // Using array-indexing works as expected here
  return 0;
}

答案 1 :(得分:1)

pint (*p)[2][2];中的指向数组的指针,当您尝试打印时,它不是2D array

替换

printf("Four: %p\n", s.p[1][1]);/** %p is format specifier to prints the address, Instead use %d to print value **/

printf("Four: %d\n", (*s.p)[1][1]);

答案 2 :(得分:0)

是包含指向二维数组的指针的结构。因此,使用“s.p”,您将获得指向二维数组的指针。考虑到:

(*(s.p))[1][1]

将为您完成工作。

我猜你不打算使用%p占位符,因为它用于打印地址。

相关问题