Announcement

Collapse
No announcement yet.

Stupid question: How to copy a buffer string?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stupid question: How to copy a buffer string?

    I have been toying with this for a while and I am not having much luck.
    I am using an I2C protocol and printing data on the screen. I want to only update the screen if data changes and is different from the last received data.

    I am using something like this to save the last received string into memory, but my if statement I use for comparison always triggers regardless.
    Buffer prints to the screen exactly what I want on the screen, but screendesc does not print anything.
    What am I doing wrong? It's gotta be some pointer or something dumb.

    I2C_Idle();
    I2C_Start();
    I2C_Write(0x09);
    I2C_Idle();

    I2C_Gets(buffer,48);
    I2C_Nack(); // finished reading, send Not Ack
    I2C_Stop(); // finished with bus

    if(buffer != screendesc)
    gfx_MoveTo(20, 150);
    putstr(buffer);
    str_Copy(screendesc, buffer);
    putstr(screendesc);
    endif

  • #2
    You can't compare strings that way, you are comparing the addresses of the strings, which will pretty much always be different.

    You need to use str_Match()
    Mark

    Comment


    • #3
      I understand the compare, but how come I cannot copy my original string into the screendesc memory?
      My putstr(screendesc) always comes back empty.

      Comment


      • #4
        I think what you need to fix your problem is to use
        Code:
            str_Copy(str_Ptr(screendesc),str_Ptr(buffer)) ;
        rather than what you have, that assumes that both those variables are defined as arrays.

        Perhaps have a read of http://www.4dsystems.com.au/appnote/4D-AN-00056/
        Mark

        Comment


        • #5
          OK, I am almost there.

          I modified my code:
          p1 := str_Ptr(msg);
          p2 := str_Ptr(screendesc);
          n := str_Match(&p1, &p2);

          if(n == 0)
          putstr(msg);
          str_Copy(str_Ptr(screendesc), str_Ptr(msg));
          endif

          My if statement is always triggering, because n always equals 0.
          I ran a test statement, below. &p1 = 421 and &p2 = 422. Perhaps I am cutting off the null terminator?

          to(buffer); print("n:", n, " s:", &p1, " h:", &p2);
          p := str_Ptr(buffer);
          str_Printf (&p, "%s");

          My debug message returns: n:0 s:421 h:422.

          The str_Copy command seems to work otherwise, as I need.
          Last edited by DB_IQ; 16th December 2015, 04:42 AM.

          Comment


          • #6
            You only need
            Code:
            n := str_Match(&p1, p2);
            The first parameter is altered, hence the need for the & operand, the second isn't
            Mark

            Comment

            Working...
            X