Thread: strcmp help please

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    36

    strcmp help please

    I cannot seem to get the following command to trigger. I see the UART characters on a digital analyzer so I know the characters are there. pRx gets updated in a UART ISR, therefore the volatile....Can someone please tell me if I have the types specified accordingly?

    Code:
    volatile char *pRx;const char* pTx;
    Code:
    const char* testcmd = "ER_CMD#P?";
    Code:
        pTx = testcmd;
    Code:
    if (strcmp((const char*)pTx, (const char*)pRx) == 0) {

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,986
    First thing to check is whether your input string is \0 terminated before you try to strcmp it.

    None of the casting will help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    36
    When I look at expression testcmd in the debug window I do NOT see a \0 at the tail...The pRx is a pointer to a volatile char incoming[20]....This array is oversized and filled with 0x00....So I am guessing I am not terminated correctly....Can you tell me how to do that correctly?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,986
    Show your receiving code that appends to the buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    36
    Code:
    *(&incoming)	unsigned char[20]	[69 'E',82 'R',95 '_',67 'C',77 'M'...]	0x002000	
    	[0]	unsigned char	69 'E'	0x002000	
    	[1]	unsigned char	82 'R'	0x002001	
    	[2]	unsigned char	95 '_'	0x002002	
    	[3]	unsigned char	67 'C'	0x002003	
    	[4]	unsigned char	77 'M'	0x002004	
    	[5]	unsigned char	68 'D'	0x002005	
    	[6]	unsigned char	35 '#'	0x002006	
    	[7]	unsigned char	80 'P'	0x002007	
    	[8]	unsigned char	57 '9'	0x002008	
    	[9]	unsigned char	0 '\x00'	0x002009	
    	[10]	unsigned char	0 '\x00'	0x00200A	
    	[11]	unsigned char	0 '\x00'	0x00200B	
    	[12]	unsigned char	0 '\x00'	0x00200C	
    	[13]	unsigned char	0 '\x00'	0x00200D	
    	[14]	unsigned char	0 '\x00'	0x00200E	
    	[15]	unsigned char	0 '\x00'	0x00200F	
    	[16]	unsigned char	0 '\x00'	0x002010	
    	[17]	unsigned char	0 '\x00'	0x002011	
    	[18]	unsigned char	0 '\x00'	0x002012	
    	[19]	unsigned char	0 '\x00'	0x002013

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    933
    Am I missing something here?
    Are you expecting strcmp("ER_CMD#P?", "ER_CMD#P9") to be 0?
    The are obviously not equal: '?' != '9'

    Maybe you mean to do something more like this:
    Code:
    char buf[512];
     
    fgets(buf, sizeof buf, ???);
     
    const char *xyz = "ER_CMD#P";   // doesn't have the question mark
     
    if (strncmp(buf, xyz, strlen(xyz)) == 0)
    {
        int n = buf[strlen(xyz)] - '0';
    }
    Last edited by john.c; 04-07-2020 at 01:06 PM.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2017, 01:36 AM
  2. strcmp()
    By Chinchila in forum C Programming
    Replies: 26
    Last Post: 11-02-2012, 01:09 PM
  3. strcmp
    By bob1223 in forum C Programming
    Replies: 4
    Last Post: 08-29-2010, 08:24 AM
  4. strcmp???
    By SvNo7 in forum C Programming
    Replies: 7
    Last Post: 12-30-2006, 04:34 PM
  5. What does strcmp actually do?
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 01:32 PM

Tags for this Thread