This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

changing value using array of pointers

Dear Sir,

I am declaring a pointer to a string as
unsigned char *sTemperature [4] = {"NIL","25C ","30C ","37C "};

Note: I cannot use a 2-dimentional array here.

Now I wish to write "25F" instead of "25C" i.e. I want to replace 'C' by 'F'. I am unable to do
it. I tried to do this with statement
STemperature[2][2] = 'F' but this doesn't work.

How should I implement this.

Regards,

Mohit

  • I guess that the array is located in the code segment, because it is a constant. Check your linker report for it (*.m51). Probably the pointer (if it is a generic pointer - 3 byte) points to code memory, so it might be better to change the whole string and not just one character of it.

    Take care
    Sven

  • Looks to me as if this should work. Althow the index may be wrong (sTemperature[1][2] instead of sTemperature[2][2]). And you have to make sure that the strings reside in RAM so that you could modify them.

  • Mohit,

    Your defintion:

    unsigned char *sTemperature [4] = {"NIL","25C ","30C ","37C "};

    creates an array of pointers to string literals (or string constants). The rules of C state that a string literal is read-only and connot be modified. You have created an internal static array.

    You will need to create a new definition for sTemperature.

  • Here is a definition that will work.

    static unsigned char sTemp1[] = "NIL ";
    static unsigned char sTemp2[] = "25C ";
    static unsigned char sTemp3[] = "30C ";
    static unsigned char sTemp4[] = "37C ";
    unsigned char *sTemperature[] = {sTemp1,sTemp2,sTemp3,sTemp4};