error C2099: 初始值设定项不是常量

来源:互联网 发布:windows 垃圾清理 编辑:程序博客网 时间:2024/05/22 04:31
//.c报错,.cpp不报错。
#include <stdio.h>
#include <stdlib.h>
int a = 1;
int b = a;
void main()
{
    printf("%d\n", b);
    system("pause");
}

#include <stdio.h>
#include <stdlib.h>
int* p = (int *)malloc(10*sizeof(int));
void main()
{
    *p = 10;
    printf("%d\n", *p);
    system("pause");
}
因为c编译器不支持函数外动态声明变量和分配空间,如果要必须是常量值,例如下面也不行,只要放到函数中就行
#include <stdio.h>
#include <stdlib.h>
int* p = (int *)malloc(10*sizeof(int));
void main()
{
    *p = 10;
    printf("%d\n", *p);
    system("pause");
}
0 0
原创粉丝点击