C语言const应用问题
#include<stdio.h>
const int sou[4]={1,2,3,4};
void move(const int a[],int b[]);
int main(void)
{
int rat[4];
int i,j,k,l;
i=sou[0];
j=sou[1];
k=sou[2];
l=sou[3];
move(sou,rat);
printf("i=%d\n j=%d\n k=%d\n l=%d\n rat[0]=%d\n rat[1]=%d\n rat[2]=%d\n rat[3]=%d\n",i,j,k,l,rat[0],rat[1],rat[2],rat[3]);
return 0;
}
void move(const int a[],int b[])
{
for(int c=0;c<4;c++)
a[c]=b[c];
}
VC++6下编译就会显示:D:\C 实例练习\kk\kk.cpp(20) : error C2166: l-value specifies const object
执行 cl.exe 时出错.
这个信息,
请问各位朋友,这个程序的问题是出在哪里?
[解决办法]
#include<stdio.h>
int sou[4]={1,2,3,4};
void move(int a[],const int b[]);
int main(void)
{
int rat[4];
int i,j,k,l;
i=sou[0];
j=sou[1];
k=sou[2];
l=sou[3];
move(sou,rat);
printf("i=%d\n j=%d\n k=%d\n l=%d\n rat[0]=%d\n rat[1]=%d\n rat[2]=%d\n rat[3]=%d\n",i,j,k,l,rat[0],rat[1],rat[2],rat[3]);
return 0;
}
void move(int a[],const int b[])
{
for(int c=0;c<4;c++)
a[c]=b[c];
}