CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

it is possible to delete a string?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

it is possible to delete a string?
PostPosted: Mon May 02, 2016 10:10 am     Reply with quote

Hy.
I try to renew string "J"
after using once function "strcmp", "J" have old data +new data.
question:
how can delete or renew string "J" after A=0?

Code:
char I[] ={"test"};
/////////////////////////////////
A = strcmp ( I , J );
if (A==0)
   {
   printf(" %D  ", A);
   }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 02, 2016 10:12 am     Reply with quote

Just set the first byte of the array to 0x00. This will "delete" the string.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Mon May 02, 2016 10:28 am     Reply with quote

like this?
J[0]=0x00;
is not working.

Code:
A = strcmp ( I , J );
if (A==0)
   {
   printf(" %D  ", A);
   J[0]=0x00;
   }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 02, 2016 10:44 am     Reply with quote

How do you know it's not working ? Post the code that shows it's not
working. And tell me what you see when you run the code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 02, 2016 10:55 am     Reply with quote

How do you know?.
Nothing you post shows what you then try to do with the string.

The _variable_ will still exist, but used as a string (so any of the string functions), it will then be an empty string.

Remember if this is inside a function, it'll be re-loaded whenever the function loads. Have switched this to I rather than J for a demo.
Code:

//Obviously suitable processor header, clock etc..

int1 demo(char * J)
{
    char I[] ="test";
    int1 A;
    A = strcmp ( I , J );
    if (A==0)
    {
        I[0]='\0';
    }
    return A;
}

//every time you call this, I[] will be reloaded with "test"

void main(void)
{
    char test_compare[]="test"

    if (demo(test_compare))
        printf("Match\n");
    else
        printf("No match\n");
    //will print "Match"
    if (demo(test_compare))
        printf("Match\n");
    else
        printf("No match\n");
    //will printf "Match" again
}

//However if you change the declaration of demo to:
int1 demo(char * J)
{
    static char I[] ="test";
    int1 A;
    A = strcmp ( I , J );
    if (A==0)
    {
        I[0]='\0';
    }
    return A;
}

//It'll print "No match" the second time, as the variable is not re-loaded


Using the second 'demo' version, the string gets cleared after the first match, and is not then reloaded (static declarations are only loaded once at the start), so the second call then does not match.

I see PCM_programmer posted exactly the same first line, while I was typing!... Very Happy
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Mon May 02, 2016 10:59 am     Reply with quote

I cant print entire code because is huge.
after J "test" is the same with I, show zero on display but with new "test" is not showing something else not even zero...
Code:

printf("%s ", J);
A = strcmp ( I , J );
if (A==0)
   {
   printf(" %D  ", A);
   J[0]=0x00;
   }
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 02, 2016 11:04 am     Reply with quote

strcmp, will return '0' for a match, or 1/-1 according to whether the strings are alphabetically less than or greater than each other. So 'of course' it'll return a non zero value. It'll no longer match....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 02, 2016 11:04 am     Reply with quote

Where is the code after you set J[0] = 0x00 ? That's the important
code, but you didn't show it.

Show the code where you use the J[] array, after the first byte is set to 0.
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Mon May 02, 2016 11:15 am     Reply with quote

this is the code after infinite loop

Code:
if(kbhit()){
key = getc();
putc(key);
INDEX_COMAND=1;
         
if (key==13)      //enter
            {
            printf("\r\n");   
            G=0;             
            J[0] = '\0';
            }
         }
     
      if (key!=key2){
         J[G]=key;
         key2=key;         
         if (G<7){
            G++;
            }
         printf("\r\n");   
         printf("%s ", J);

         }

       
         if (INDEX_COMAND<3)
            {
            INDEX_COMAND++;
            if (INDEX_COMAND==2)
               {
               INDEX_COMAND=250;
               COMP_COMAND = strcmp ( J , I );
               if (COMP_COMAND==0) {
                printf(" %D  ", COMP_COMAND);
                 J[0] = '\0';
                 }
               }
            }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 02, 2016 11:26 am     Reply with quote

Quote:

G=0;
J[G]=key;
printf("%s ", J);

After you do this, you have to make J[] into a string before you print it.
You have to write a 0x00 after the text bytes.

You have to do this:
Quote:
G=0;
J[G]=key;
J[G+1] = 0x00;
printf("%s ", J);
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 02, 2016 11:50 am     Reply with quote

Just to explain.

In C, a 'string', is an array of characters _terminated with a NULL_ ('\0' or 0x00).

This is why you can 'clear' a string by putting a terminator at the start.

Your array needs a terminator.

Without this, the print will carry on printing stuff from memory, until it comes across a zero. So could be anything.....
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Mon May 02, 2016 1:37 pm     Reply with quote

Thank you for supplementary explanation.
As you say for printf is working but for strcmp is not working.
I'm interested for strcmp to work, printf is only for verifying.

What is happening?
Word is cleared when is "printed"
but after strcmp I have at:
one moment more 1's (because compaction f string)
single moment 0 (string are the same)
and -1 (string are not the same)
press enter
and again lot of 1's and nothing 0 or -1.
That's I'm saying is working half.
Can help me?
Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 02, 2016 2:29 pm     Reply with quote

Read what I posted earlier about strcmp.

strcmp, returns -1, 0, or 1 according to whether the first different character is greater than or less than the other. So '1' is a perfectly legitimate 'different' answer. How it would handle the first character being NULL, is arguable. It is actually better if this might ever be the case, to test this before calling strcmp (significantly quicker....).
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 02, 2016 2:33 pm     Reply with quote

if strcmp() doesn't work, then add some debugging code to display both
strings in hex format. Look at the data that was printed. Make sure
the text has a 0x00 after the last text character. Make sure the text
is what you expected it to be in both strings. Example:
Code:

int8 index;
signed int8 result;

// Display the I string as hex bytes.
for(index = 0; index < sizeof(I); index++)
    printf("%x ", I[index]);
printf("\n\r");
 
// Display the J string as hex bytes.
for(index = 0; index < sizeof(J); index++)
    printf("%x ", J[i]);
printf("\n\r");

// Now do the string comparison.
result = strcmp(I, J);

printf("strcmp result = %d \n\r", result);
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Tue May 03, 2016 1:55 pm     Reply with quote

yes PCM programmer thank you for your debugging solution.
I found where was the problem:
problem was because G are not making zero, are making 1 because after G=0 is G++ so when G=0 is placed where it should be placed code is working perfectly.

Ttelmah thank you for your support I read and applied your solution but I do not found where was the problem.

So you both had a support for me and thank you again so much for this.
Best regards
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group