简体   繁体   English

使用指针算法打印 3D 数组元素的指针

[英]Printing a pointer of an 3D array element using pointer arithmetics

I am testing how to use array name with pointer arithmetic to access an array elemnets and I have come up with this program:我正在测试如何使用带有指针算法的数组名来访问数组 elemnets,我想出了这个程序:

#include <stdio.h>

int main(){

    // definition of array using designators
    int a[2][2][2] = {
        [0] = {[0] = {1, 2}, [1] = {3, 4}},
        [1] = {[0] = {5, 6}, [1] = {7, 8}}
    };

    printf("7th  element (pointer):  %p\n", *(*(a + 1) + 1) + 0);
    printf("8th  element (pointer):  %p\n", *(*(a + 1) + 1) + 1);

    return 0;
}

Although program works and prints everything correct:尽管程序可以正常工作并打印正确:

7th  element (pointer):  0x7ffd4b09a1c8
8th  element (pointer):  0x7ffd4b09a1d0

I get warnings at compile time saying something like this for every line where I use %p place holder inside printf() :我在编译时收到警告,对于我在printf()中使用%p占位符的每一行都说这样的话:

warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int *’ [-Wformat=]
  printf("7th  element (pointer):  %p\n", *(*(a + 1) + 1) + 0);
                                   ~^     ~~~~~~~~~~~~~~~~~~~
                                   %ls

So at the first glance it looks like I have to simply cast the pointers to (void *) and if I do this and change line:因此,乍一看,我必须简单地将指针转换为(void *) ,如果我这样做并更改行:

printf("8th  element (pointer):  %p\n", *(*(a + 1) + 1) + 1);

With this line:有了这条线:

printf("8th  element (pointer):  %p\n", (void *)(*(a + 1) + 1) + 1);

I get a warning:我收到警告:

warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
  printf("8th  element (pointer):  %p\n", (void *)(*(a + 1) + 1) + 1);
                                                             ^

and compiler calculates a different address for 8'th element and this address is only 1 byte larger than preceeding elemnet's address:并且编译器为第 8 个元素计算一个不同的地址,这个地址只比前面的 elemnet 的地址大 1 个字节:

7th  element (pointer):  0x7ffd9e5e1bf8
8th  element (pointer):  0x7ffd9e5e1bf9

I also tried to fix it like this (added one more braces) :我也尝试像这样修复它(添加了一个大括号)

printf("8th  element (pointer):  %p\n", (void *)((*(a + 1) + 1) + 1));

and warning was gone, but address is still calculated differently:警告消失了,但地址的计算方式仍然不同:

7th  element (pointer):  0x7ffca6c6c468
8th  element (pointer):  0x7ffca6c6c470

It looks like compiler needs pointer type to calculate the addresses and if I cast it it will not calculate the address corectly.看起来编译器需要指针类型来计算地址,如果我转换它,它不会正确计算地址。 Does anyone have any idea what I can do to remove the warning and get address calculated in a right way?有谁知道我可以做些什么来删除警告并以正确的方式计算地址?

If you need the arithmetic to be on int* , and the value to be look at as void* , cast the value after you're doing the arithmetics:如果您需要在int*上进行算术,并且将值视为void* ,请在执行算术转换该值:

printf("7th  element (pointer):  %p\n", (void *)((int*)*(*(a + 1) + 1) + 0));
printf("8th  element (pointer):  %p\n", (void *)((int*)*(*(a + 1) + 1) + 1));

You can store the pointer into a separate variable and pass it to printf:您可以将指针存储到单独的变量中并将其传递给 printf:

void *ptr7th = (*(*(a + 1) + 1) + 0);
void *ptr8th = (*(*(a + 1) + 1) + 1);
printf("7th  element (pointer):  %p\n", ptr7th);
printf("8th  element (pointer):  %p\n", ptr8th);

using gcc version 9.3.0 with the -Wall parameter does not return any warning.使用带有-Wall参数的 gcc 版本 9.3.0 不会返回任何警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM