Search code examples
arrayscmikroc

How to access a specific element of an array in MikroC


I have this array in MikroC:

         char array[4] = {'1','1','0','\0'};

I'm trying to get a specific element in this array and outputting it on a GLCD, let's say the first element. How can I do it? I know it should be something like that:

         Glcd_Write_Text(array[0], 5, 4, 2);

but this gives no output at all or maybe some random garbage. Hence, I tried working with pointers as follows:

         Glcd_Write_Text(&array[0], 5, 4, 2);

but it gives the whole array and I need the first element only. I tried that too:

                      int *v=&array[0];
                      char y=*v;

but outputting y gives random garbage data. Any help is greatly appreciated. Thanks a lot.


Solution

  • If Glcd_Write_Text expects the argument to be a string, you can't give it a pointer to a single character. It expects a pointer to a null-terminated string.

    Declare a new array and copy the specific element to its first character.

    char text[2] = {'\0', '\0'};
    text[0] = array[0];
    Glcd_Write_Text(text, 5, 4, 2);