Search code examples
ctcpconstantsbinaryfiles

How to change an array if it is const?


This program reads and writes on a binary file. The professor is asking to switch the value of the source port and destination port. But I know we can not change if it is const, I also get this error. Does anyone know what he means by that or have any idea how I can switch? Thanks a lot.

error: assignment of read-only location ‘*array’

/*array = the array the data is stored.*/
void header(const unsigned char array [], unsigned char filename [])
{
   
}

 

Solution

  • You can do this through pointers:

    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   
    }