運行下面的程序後:能夠常量指針更改值
gcc -c volVars.c -o volv
./volv
它編譯。
#include<stdio.h>
void main(){
printf("study of volatile pointers\n");
const int lConstInt=6;
printf("\n const int is %d\n",lConstInt);
volatile const int *lvcint=&lConstInt;
printf("volatile const int after assignment = %d\n",*lvcint);
//*lvcint=*lvcint+1; uncommenting this gives compilation error
int *track = lvcint;
*track = *track + 1;
printf("modified the lcoation = %d\n",*track);
}
如果我取消註釋lvcint=*lvcint+1;
行它給出了預期的錯誤。但如果我使用非const的軌道引用指針(lvcint
),我可以修改其內容。我在該行發出警告,但最終我能夠修改只讀位置的內容。 gcc中是否有任何錯誤或者我缺少某些東西。
不錯閱讀:[如何改變C++中的變量的常量?](http://stackoverflow.com/questions/13618706/how-to-change-the-constness-of-a-variable-in- c) –
每個人都需要注意編譯器的警告。 –
符合C編譯器只有在違反規則時才被迫發出「診斷」。你說你看到一個警告,那麼這是你的錯,不要忽視這個警告。 –