-1

I am transmitting and receiving between a terminal on my phone and a bluetooth module connected to a microcontroller (pic18f).

I set up an array of 4, char input[4] = "" to store the user input from the terminal. The function getsUSART(input,4) places input in there.

The problem is, when I check what is in input, I see that this is 'ABC/UN' when I type ABC in the terminal. Is /UN an escape sequence? If so, what key is it for?

Also, if I'd like to compare input to a string, let's say the string is 'hey' would I do char str1[4] = "hey/UN" and then strcmp(input, str1) == 0?

Edit:

So the compiler library (USART) can be accessed here, page 66 I believe: http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB_C18_Libraries_51297f.pdf

This is the relevant code, just for getting the input and putting null:

    char input[4] = "";
    while(BusyUSART());
    while (!DataRdyUSART());
    getsUSART(input, 4);            //Get the input. 
    // NULL terminate the string for putsUSART call.
    input[4] = '/0'; 
    putrsUSART("Input:");
    putsUSART(input);

I am trying to null terminate it when I do input[4] = '/0', am I doing something wrong here if it isn't null terminating?

Lukali
  • 343
  • 1
  • 4
  • 15
  • Can you please put your whole program with the definitions of all functions like getsUSART. – Forever Learner Apr 23 '18 at 13:12
  • 3
    Most likely your string is not null terminated, and `/UN` is just arbitrary memory contents. Please show your source code and documentation for `getsUSART`. – Retr0id Apr 23 '18 at 13:12
  • @Retr0id Okay I've added it in. I try to null terminate by input[4] = '/0' – Lukali Apr 23 '18 at 13:25
  • 1
    `'/0'` is not valid C. Read your compiler's warnings/errors – Retr0id Apr 23 '18 at 13:25
  • And `input[4]` is beyond the stated range for `input`. (Which is `0..3`.) – Jongware Apr 23 '18 at 13:25
  • When I compile it doesn't give me any warnings or errors on that line..? How would I null terminate a string in this case then? So should I make my null terminate be at input[3] usr256? Arrays confuse me like hell... If I can only use 0 to 3 then what exactly is 4 there for? – Lukali Apr 23 '18 at 13:28
  • 3
    @Retr0id Technically, `'/0'` _is_ valid C (see [Multiple characters in a character constant](https://stackoverflow.com/q/6944730/2096401)), it's just that what happens is (a) implementation defined and (b) for any plausible implementation, not what the OP wants. – TripeHound Apr 23 '18 at 13:37
  • @TripeHound wow... – Retr0id Apr 23 '18 at 13:42

1 Answers1

0

You are forgetting C counts from 0.

So in your example:

   input[0] = 'A'
   input[1] = 'B'
   input[2] = 'C'
   input[3] = some old byte of data
   input[4] = '\0'
James Anderson
  • 27,109
  • 7
  • 50
  • 78
  • Not to mention having `/0` instead of `\0`. – TripeHound Apr 23 '18 at 13:32
  • So if I want to take in 3 characters, I would just need input[3]..? And input[3] = '\0'? But I am getting "null-terminated initializer string too long" when I try to do char str1[3] = "ABC", so what's happening here? – Lukali Apr 23 '18 at 13:35
  • 1
    You seem to be conflating many problems. "ABC" requires 4 bytes of storage, because of the null terminator. – Retr0id Apr 23 '18 at 13:39
  • 1
    @Lukali No, you'd need `input[4]` so there are three "boxes" for characters (`input[0]`, `input[1]` and `input[2]`) and one for the terminating `'\0'` (`input[3]`) -- four in total. – TripeHound Apr 23 '18 at 13:40
  • 1
    @TripeHound Thanks you made it so much clearer. I thought input[3] meant that there was 0,1,2,3 so 4 inputs. I see my mistake now. – Lukali Apr 23 '18 at 13:55