0

I have two char arrays of different lengths. I want to make sure that the first 256 elements are same.

I am using the following:

for (int i = 0; i < 256; i = i + 1) {
  if (arr1[i] != arr2[i]) {
    not_equal = 1;
  }
}

Will this catch all cases of special characters etc.?

6
  • 1
    Yes, it will catch all the cases. Commented Apr 16, 2022 at 18:13
  • Use strncmp() or memcmp() if you don't want the indexes of mismatch. Commented Apr 16, 2022 at 18:18
  • 2
    It is usual to break out once the first mismatch is found - there is no point in indexing any further. Commented Apr 16, 2022 at 19:48
  • 2
    You must ensure both arrays begin with at least 256 initialized values, or this will invoke undefined behaviour. Commented Apr 16, 2022 at 21:40
  • 1
    @MartinJames "there is no point in indexing any further." --> Mostly true: perhaps, unless one is trying to code with equal run time - something important with code that should not leak timing info in crypto applications. Of course this code does not insure equal time for various reasons. Commented Apr 17, 2022 at 12:30

1 Answer 1

1

Will this catch all cases of special characters etc.?

Yes, except:

  • If either array is less than 256 elements, code attempts to access outside array bounds - which is undefined behavior (UB). @Oka

  • arr1[i] != arr2[i] can fail when char is signed and uses non-2's compliment encoding (think -0). Not much risk of that theses days. Solution: access data as unsigned char. ((unsigned char *)arr1)[i] != ((unsigned char *)arr2)[i].

Sign up to request clarification or add additional context in comments.

3 Comments

What's this comment about signed char? In what specific case can it fail? It looks like the if statement is comparing two chars for equality and signedness or representation is irrelevant.
@PaulHankin "think -0": A -0 has a different bit pattern than +0, even though they have the same arithmetic value. It is a question of same-ness for a nearly lost form of integer encoding. With C library character function treat -0 different than +0. "For all functions in this subclause, each character shall be interpreted as if it had the type unsigned char (and therefore every possible object representation is valid and has a different value)."
I see, yes that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.