寒假软设--小型数据库进展(1)----error C2099: initializer is not a constant 或者error C2099:初始值设定项不是常量
先看一道题:#include<stdio.h>const int a=15;const int b=a+15;void main(){printf("%d\n",b);}很有意思哟:在.c文件中报的是和你一样的,初始值设置不是常量,但在.cpp中可以,结果输出30改成这样就行#include<stdio.h>void main(){ const int a=15;const int b=a+15;printf("%d\n",b);}因为c编译器不支持函数外动态声明变量和分配空间,如果要必须是常量值,例如下面也不行,只要放到函数中就行#include<stdio.h>int c=16;int d=c;void main(){ const int a=15;const int b=a+15;printf("%d\n",b);}同样的下面也是#include <stdio.h>#include <malloc.h>#define SIZE 4float *p=NULL;p=(float *)malloc(SIZE*sizeof(float));int main(){int i=0;for(i=0;i!=SIZE;++i)p[i]=99;for(i=0;i!=SIZE;++i)printf("%f\n",p[i]);return 0;}要改成下面这样:#include <stdio.h>#include <malloc.h>#define SIZE 4int main(){int i=0;float *p=NULL;p=(float *)malloc(SIZE*sizeof(float));for(i=0;i!=SIZE;++i)p[i]=99;for(i=0;i!=SIZE;++i)printf("%f\n",p[i]);return 0;}//看看下面有意思的#include<stdio.h>//static int b;int fun(){int a=15;static int b;b=a;b++;return b;}//void print()//{b++;//printf("%d\n",b);//}//void print1()//{//printf("%d\n",b);//}void main(){printf("%d\n",fun());printf("%d\n",fun());/*print();print1();*/}这样的话全为16,原因是static int b;第一次初始化为0,以后不会初始化为0了,而下面这样的话结果是16和17,元素师static int b=a;初始化为15,以后不会初始化为15了。#include<stdio.h>//static int b;int fun(){int a=15;static int b=a;b++;return b;}//void print()//{b++;//printf("%d\n",b);//}//void print1()//{//printf("%d\n",b);//}void main(){printf("%d\n",fun());printf("%d\n",fun());/*print();print1();*/}但是如果上面的这段代码出现在.c文件,记住哟不是.cpp文件中就会出现类似的错误:error C2099:初始值设定项不是常量,解决就是把static int b;定义到函数外同样一段在.cpp文件中和.c文件不一样:#include<iostream>#include<string.h>#include<stdio.h>using namespace std;typedef void * (*pFun)(void *,int);void * intMax(void * arg,int nCount){ int * agu=(int *)arg;static int Max=*agu;for(int i=1;i<nCount;i++)if(Max<*(agu+i))Max=*(agu+i);elsecontinue;return &Max;}void * floatMax(void * arg,int nCount){ float * agu=(float *)arg;static float Max=*agu;for(int i=1;i<nCount;i++)if(Max<*(agu+i))Max=*(agu+i);elsecontinue;return &Max;}void * doubleMax(void * arg,int nCount){ double* agu=(double *)arg;static double Max=*agu;for(int i=1;i<nCount;i++)if(Max<*(agu+i))Max=*(agu+i);elsecontinue;return &Max;}void * charMax(void * arg,int nCount){ char* agu=(char *)arg;static char Max=*agu;for(int i=1;i<nCount;i++)if(Max<*(agu+i))Max=*(agu+i);elsecontinue;return &Max;}void main(){ /* int a[]={3,2,56,45,78};void * result=intMax(a,5);cout<<*((int *)result)<<endl;float a[]={3.34f,2.34f,56.45f,45.32f,78.3456f};void * result=floatMax(a,5);cout<<*((float*)result)<<endl;*/double a[]={3.34,2.34,56.45,45.32,78.3456};void * result=doubleMax(a,5);cout<<*((double*)result)<<endl;char a1[]={'A','a','h','f','B'};void * result1=charMax(a1,5);cout<<*((char*)result1)<<endl;}如果改到.c文件就会出错:改正如下:#include<string.h>#include<stdio.h>typedef void * (*pFun)(void *,int);void * intMax(void * arg,int nCount){ int * agu=(int *)arg;int i;int pos;int Max=*agu;for( i=1;i<nCount;i++)if(Max<*(agu+i)){Max=*(agu+i);pos=i;}elsecontinue;//return &Max;return (void*)(agu+pos);;//解决返回局部地址警告}void * floatMax(void * arg,int nCount){ float * agu=(float *)arg;float Max=*agu;int i;int pos;for( i=1;i<nCount;i++)if(Max<*(agu+i)){Max=*(agu+i);pos=i;}elsecontinue;// return &Max;return (void*)(agu+pos);;//解决返回局部地址警告}void * doubleMax(void * arg,int nCount){ double* agu=(double *)arg;double Max=*agu;int i;int pos;for( i=1;i<nCount;i++)if(Max<*(agu+i)){Max=*(agu+i);pos=i;}elsecontinue;return &Max;//return (void*)(agu+pos);;//解决返回局部地址警告}void * charMax(void * arg,int nCount){ char* agu=(char *)arg;char Max=*agu;int i;int pos;for( i=1;i<nCount;i++)if(Max<*(agu+i)){ Max=*(agu+i);pos=i;}elsecontinue;return &Max;补充:综合编程 , 其他综合 ,
上一个:基数排序(radix sort)
下一个:as3事件流机制彻底理解