如果它是 const,如何更改数组?
How to change an array if it is const?
这个程序读写一个二进制文件。教授要求切换源端口和目标端口的值。但我知道如果它是常量我们就不能改变,我也得到这个错误。有谁知道他的意思,或者知道我该如何切换?非常感谢。
error: assignment of read-only location ‘*array’
/*array = the array the data is stored.*/
void header(const unsigned char array [], unsigned char filename [])
{
}
你可以通过指针来做到这一点:
void header(const unsigned char array [], unsigned char filename [])
{
char *array2 = (char *)array;
// You can freely modify array2 elements here, resulting in the original array's modification
}
这个程序读写一个二进制文件。教授要求切换源端口和目标端口的值。但我知道如果它是常量我们就不能改变,我也得到这个错误。有谁知道他的意思,或者知道我该如何切换?非常感谢。
error: assignment of read-only location ‘*array’
/*array = the array the data is stored.*/
void header(const unsigned char array [], unsigned char filename [])
{
}
你可以通过指针来做到这一点:
void header(const unsigned char array [], unsigned char filename [])
{
char *array2 = (char *)array;
// You can freely modify array2 elements here, resulting in the original array's modification
}