下标具有寄存器存储 class 的数组

subscript an array having the register storage class

在 GNU c99 手册中:

3.13 Array Subscripts

You can access array elements by specifying the name of the array, and the array subscript (or index, or element number) enclosed in brackets. Here is an example, supposing an integer array called my_array:

my_array[0] = 5;

The array subscript expression A[i] is defined as being identical to the expression (*((A)+(i))). This means that many uses of an array name are equivalent to a pointer expression. It also means that you cannot subscript an array having the register storage class.

我已经尝试了以下方法,对我来说效果很好。

#include <stdio.h>

int main()
{
        register int arr[10] = {1, [9] = 6};

        printf("arr[9] %d\n", *((arr) + 9)); // working fine
        printf("arr[9] %d\n", arr[9]); //working fine
        return 0;
}

有人可以解释一下粗体字的声明是什么意思吗?或者我验证错误了。

GCC 8.1 产量:

$ gcc rega89.c
rega89.c: In function ‘main’:
rega89.c:7:9: error: address of register variable ‘arr’ requested
         printf("arr[9] %d\n", *((arr) + 9)); // working fine
         ^~~~~~
$

没有任何额外选项。事实上,即使是 GCC 4.8.1 也没有选项。 (GCC 8.1 默认为 C11;GCC 4.8.1 默认为 C90;指定 -std=c99-std=gnu99 没有区别。)

因此,如果您使用的是 GCC,则必须使用比 4.8.1 更旧的 GCC 版本。或者您使用的其他编译器不像 GCC 那样符合标准。