-4

I try to create two function to split string become 2 part , the first function i want to get string "Mode 1", from serial port when RX_IN_CHR_UART variable, filled data like "Mode 1 34", and the second function i want to get the "34"

this function to get string "Mode 1"

    #define GET_MODE_X 6
    #define GET_VALUE 4
    const char RX_IN_CHR_UART[];
        char* pod(char buf_Rx[])
            {
              int i=0;
              char spn[GET_MODE_X]; // 7
              for(i=0;i<GET_MODE_X;i++)
              {
                spn[i] = buf_Rx[i];
              }
              return Get_mode_x = spn;
            }

and this function to get string "34"

char* dop(char buf_Rx_val[])
{
   int k=0; 
   char datasip[GET_VALUE]; //2       
   for(k=0;k<GET_VALUE;k++)
   {
     datasip[k] = buf_Rx_val[GET_MODE_X+k+1];
   }
   return (Get_val_mod = datasip);
}

when i try to compare function dop() use if(dop(RX_IN_CHR_UART)=="34") it doesn't works.

 if(!strcmp(mode1,pod(RX_IN_CHR_UART))) // compare "Mode 1"
   {
      Send_teks("Mode 1 ->:"); // send to serial port
      Send_teks(pod(RX_IN_CHR_UART));
      Send_Char('\n');
      Send_teks("Data yang dimasukkan ->:");
      Send_teks(dop(RX_IN_CHR_UART));
      Send_Char('\n');
      if(dop(RX_IN_CHR_UART) == "34") // doesn't work well
      {
        pwm_off();
        LCD_command(0x85);
        LCD_sendstring("Kipas off  ");
        Send_teks("Sukses");
        Send_Char('\n');
        Send_teks("Kipas Off");
        Send_Char('\n');
      }
  }

what should i do with this function to compare dop() equal to "34" ?

Adhy
  • 92
  • 2
  • 10
  • "doesn't work" is not helpful. You need to be much more specific. – Oliver Charlesworth Jan 06 '13 at 19:51
  • You could use `strcmp`. Or, rather, you can't since the pointer returned by `dop` points to something on it's stack, which is not legal, so you would have to fix that problem first... – Lindydancer Jan 06 '13 at 19:52
  • 1
    You have several errors and undefined behaviours in your code. The undefined behaviour comes from you returning pointers to local variables in your functions. After a function returns, the memory used by local variables are reclaimed by the system, and pointers to them are no longer valid. – Some programmer dude Jan 06 '13 at 19:53
  • @Joachim Pileborg thanks I will fit it. – Adhy Jan 06 '13 at 20:14
  • possible duplicate of [What is the difference between char s\[\] and char \*s in C?](http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c) – Rais Alam Jan 07 '13 at 07:33

2 Answers2

6

Use strcmp() instead of ==, like you do in the first line.

Chris
  • 4,133
  • 30
  • 38
5

Learn to enable all warnings in your compiler. With GCC that means gcc -Wall.

A compiler should warn you that using == to compare strings (notably with a literal string) is incorrect. You should use strcmp for that purpose:

  if(strcmp(dop(RX_IN_CHR_UART), "34") == 0) { 
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • this is may be help me, but the next step i will save this value "34" to other variable to be made as "pwm" reference value. – Adhy Jan 06 '13 at 20:08
  • Judging from the tags, this is the Keil 8051 C compiler. Last time I had the misfortune to use it, not only was it somewhat primitive but also very buggy - in the sense of compiling code that wasn't quite what was intended. Checking the code with a reliable compiler is therefor an excellent suggestion. – marko Jan 06 '13 at 22:11