c - how to cast a char array to compare it with unsigned char array? -
i trying cast s2 create test pass.
i store printable characters unsigned char values s3.
s2 test string result meant verify printable char loaded in s3.
#include <stdio.h> #include <string.h> #define test_string_len 2 union { char unsigned us[test_string_len]; char s1[test_string_len]; } result; main() { char *s2; s2= "ab"; char unsigned s3[test_string_len]; s3[0] = 'a'; s3[1] = 'b'; s3[2] = '\0'; memcpy (result.us, s3, test_string_len); if ( result.s1 == s2) { printf("pass\n"); } printf("s2 = %s\n", s2 ); printf("s3 = %s\n", s3 ); printf("result.s1 = %s\n", s3 ); printf("result.us = %s\n", result.us ); getchar(); }
it's not possible compare strings (or other arrays) ==
in c. because array such int myarray[8];
can thought of pointer called myarray
storing address of first element. in other words, ==
compare starting address of array, rather contents of actual array items beingness pointed to.
instead, must either utilize strcmp or memcmp functions, or utilize for()
loop cycle through each index , check values in each array match.
c arrays
No comments:
Post a Comment