繁体   English   中英

将结构内的指针初始化为编译时常量

[英]Initialize pointer inside struct as compile-time constant

我想知道定义具有指针作为其元素之一的常量struct的合法方式是什么。 看这篇文章( Initializer element is not constant in C )我可以看到以下内容:

6.6 常量表达式

(7) 初始化器中的常量表达式允许更大的自由度。 这样的常量表达式应为或评估为以下之一:

— 算术常数表达式,

— 一个 null 指针常量,

— 地址常数,或

— object 类型的地址常量加上或减去 integer 常量表达式。

(8) 算术常量表达式应具有算术类型,且操作数应仅为 integer 常量、浮点常量、枚举常量、字符常量和 sizeof 表达式。 算术常量表达式中的强制转换运算符只能将算术类型转换为算术类型,除非作为 sizeof 运算符的操作数的一部分,其结果是 integer 常量。

我的问题是以下是否根据(C89 和 C99 的交集)标准得到了很好的定义。 test.h的内容:

#ifndef TEST_H
#define TEST_H

typedef struct _poly {
    unsigned long int degree;
    signed long int *coeffs;
} poly;

extern const poly my_poly;
extern void print_poly(poly p);

#endif

test.c的内容:

#include <stdio.h>
#include "test.h"

static signed long int coeffs[3] = {1L, 2L, 3L};
const poly my_poly = {2UL, coeffs};

void print_poly(poly p)
{
    /* Irrelevant mumbo-jumbo goes here. */
}

main.c的内容:

#include "test.h"

int main(void)
{
    print_poly(my_poly);
    return 0;
}

Compiling on Debian 11 with gcc (and -Wall -Wextra -Wpedantic -std=c89 enabled), clang (with -Weverything -std=c89 enabled), tcc (with -Wall -std=c89 enabled), and pcc (with -std=c89 enabled) 不会产生警告和错误并按预期运行。 是代码:

static signed long int coeffs[3] = {1L, 2L, 3L};
const poly my_poly = {2UL, coeffs};

初始化具有指针作为其成员之一的常量结构的正确方法,使其成为编译器时常量? 它似乎遵循它是地址常量的规则,但我不确定。

正如问题所指出的,初始化程序可能是地址常量。

C 2018 6.6 9 says “An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; 它应使用一元&运算符或转换为指针类型的 integer 常量显式创建,或通过使用数组表达式或 function 类型隐式创建……”

const poly my_poly = {2UL, coeffs}; , coeffs是 static 存储持续时间的数组,它被隐式转换为指向其第一个元素的指针(根据 C 2018 6.3.2.1 3)。 因此,转换的结果,实际上是&coeffs[0] ,是一个地址常数,可以用作初始化器。

暂无
暂无

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

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