相关疑难解决方法(0)

如何在C中初始化const中的const(使用malloc)

我试过了;

void *malloc(unsigned int);
struct deneme {
    const int a = 15;
    const int b = 16;
};

int main(int argc, const char *argv[])
{
    struct deneme *mydeneme = malloc(sizeof(struct deneme));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是编译器的错误:

gereksiz.c:3:17: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
Run Code Online (Sandbox Code Playgroud)

而且,这也是;

void *malloc(unsigned int);
struct deneme {
    const int a;
    const int b;
};

int main(int argc, const char *argv[])
{
    struct deneme *mydeneme = malloc(sizeof(struct deneme));
    mydeneme->a = 15;
    mydeneme->b = 20; …
Run Code Online (Sandbox Code Playgroud)

c malloc struct initialization constants

21
推荐指数
2
解决办法
3万
查看次数

const ++在C++中的行为

这是我的问题,问题出在评论中

const int a = 5;
const_cast<int&>(a)=7; //throw over const attribute in a,and assign to 7
std::cout<<a<<std::endl; //why still out put 5!!!!!!!!!!
Run Code Online (Sandbox Code Playgroud)

谁可以告诉我为什么,有些书籍会考虑这些问题来推荐?谢谢!

c++ const-cast undefined-behavior

5
推荐指数
1
解决办法
510
查看次数