gpt4 book ai didi

c - 错误 : initializer element is not a compile-time constant

转载 作者:行者123 更新时间:2023-12-02 10:40:38 35 4
gpt4 key购买 nike

我一直在寻找答案,但找不到任何可以运行此代码的内容。我收到 av[1]声明时,编译器在主函数中突出显示:

static char const *str = av[1];

这是我尝试使用 gcc 运行的代码:
#include <stdio.h>
#include <stdlib.h>

char *ft_strjoin(char const *s1, char const *s2);

void fct(char **av)
{
static char const *str = av[1];
str = ft_strjoin(av[1], av[1]);
printf("%s\n", str);
}

int main(int ac, char **av)
{
fct(&av[1]);
fct(&av[1]);
fct(&av[1]);
fct(&av[1]);
fct(&av[1]);
fct(&av[1]);
}

我找到了 this有趣,但我仍然不明白,也不知道如何运行这段代码。

最佳答案

报价C11 , §6.7.9, 初始化

All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.



在您的代码中,
static char const *str = av[1];
av[1]不是编译时常量值(即,不是常量表达式)。因此错误。

您需要删除 static来自 str以避免这个问题。

关于c - 错误 : initializer element is not a compile-time constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34450145/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com