Thread: memcmp not stepping into

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

    memcmp not stepping into

    Can someone tell me why the following isn't working?

    Code:
    #include <string.h>
    
    int main(void)
    {
    	int sW, mVk;
    	char permanent_Info[4] = { [0 ... 3] = 0xFF};
    	
    	sW = memcmp(permanent_Info, "FF", 4);
    	if (!sW)
    		mVk = 1;
    	else
    		mVk = 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,583
    What do you mean by "isn't working"?

    What do you expect to happen?

    What exactly are you trying to do on line 6?

    Do you know that for most current systems a char is a signed value, meaning that the largest value it can hold is 127?

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    72
    Other than using a for loop I was looking for a way to verify that permanent_Info was loaded with 0xFF. I would like the mVk =1 in the above situation and it doesn't. Actually had to move int sW, mVk; outside of scope of main as my compiler wasn't recognizing it inside the loop.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,084
    You seem to be making a few incorrect assumptions.
    0xff is not the same as "FF".
    "FF" are two 'F' character codes (70 or 0x46 in ascii or utf-8).
    Also, memcmp doesn't compare a single thing a given number of times, unlike memset which writes a single character a given number of times.
    memcmp compares two byte sequences, contiguous memory interpreted as unsigned char, of the given length.
    You would need to compare to something like "\xff\xff\xff\xff", which is actually 5 bytes long due to the terminating '\0', but you are only comparing the first 4 bytes.
    And remember that memcmp, like strcmp, will return a negative value if the first byte string comes before the second, 0 if they are equal, or a positive value if the first comes after the second. It will not necessarily return 1 for a positive value and -1 for a negative value.
    Note also that [0...3] is non-standard and a char may be (probably is) signed and so 0xff will be interpreted as -1, which may or may not make a difference depending on what you do with it. It may be best to make it unsigned.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    typedef unsigned char uchar;
     
    int main(void)
    {
        uchar data[4];
        memset(data, 0xff, sizeof data);
     
        int result = memcmp(data, "\xff\xff\xff\xff", sizeof data);
        printf("%d\n", result);
     
        if      (result == 0) printf("equal\n");
        else if (result <  0) printf("less\n");
        else                  printf("greater\n");
    }
    Last edited by john.c; 02-27-2021 at 01:50 PM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    72
    Thank you....That works. I understand your comment on 0xFF vs "FF".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcmp ?
    By ridgerunnersjw in forum C Programming
    Replies: 6
    Last Post: 01-08-2021, 12:47 PM
  2. !memcmp in set
    By thames in forum C Programming
    Replies: 2
    Last Post: 01-13-2013, 12:55 PM
  3. Stepping on people?
    By Crossfire in forum General Discussions
    Replies: 6
    Last Post: 12-28-2012, 05:04 PM
  4. Stepping Through Lines In A Buffer
    By SMurf in forum C Programming
    Replies: 18
    Last Post: 07-27-2006, 05:30 AM
  5. stepping through a for loop
    By ali1 in forum C++ Programming
    Replies: 6
    Last Post: 07-18-2004, 06:45 PM

Tags for this Thread