简体   繁体   中英

Compare string literal vs char array

I cannot find a similar answer to this.

  char buffer[] = {'a', '0'};
  char p2[] = "a";
  printf("%d", buffer==p2); // prints 0

How do I do this comparison? Is this a similar question? How do i compare a string literal to a char array.

char buffer[] = {'a', '0'};
printf("%d", buffer=="a"); // prints 0

The above are just examples, but I really need this:

 char buffer[] = {'e','a','b','g','e','l','e','g','o','n','\000','p','k','n','m','l','\000','j', 'i', 'h'};
printf("%d", buffer=="eabgelegon\000pknml\000jih");

I cannot use any other functions.

Arrays (or strings) in many circunstances, are converted to the address of their first element.

In your code, inside the printf() , these addresses are being compared.

To compare the stuff pointed to by the addresses you need strcmp() (for real strings) or memcmp() (for binary data)

char buffer[] = {'e','a','b','g','e','l','e','g','o','n','\000','p','k','n','m','l','\000','j', 'i', 'h'};

int equal1 = memcmp(buffer, "eabgelegon\000pknml\000jih", 20);
printf("%d", equal1);
int equal2 = memcmp(buffer, "eabgelegon\000XXXXXXXXX", 20);
printf("%d", equal2);

int equal3 = strcmp(buffer, "eabgelegon\000pknml\000jih");
printf("%d", equal3);
int equal4 = strcmp(buffer, "eabgelegon\000XXXXXXXXX");
printf("%d", equal4);

None of OP's 3 buffer are best described as C strings. Simply arrays of char .

char buffer[] = {'a', '0'};  // No terminating \0
char buffer[] = {'a', '0'};  // No terminating \0
// No terminating \0 yet has embedded \0
char buffer[] = {'e','a','b','g','e','l','e','g','o','n','\000','p','k','n','m','l','\000','j', 'i', 'h'};

Since OP "cannot use any other functions" ...

Find range of valid indexable memory buffer and p2 . The valid indexable range of buffer is 0 to sizeof(buffer) - 1 .

The same for p2 (the string literal) is complicated. If p2 was a typical C string, the range is 0 to strlen(p2) + 1 . But a string literal such as "eabgelegon\\000pknml\\000jih" has embedded '\\0' in it (as well as a terminating '\\0' ), so its range cannot be determined at run time with strlen() , but only at compile time with sizeof() .

Let's assume the comparison should not include the string literal's terminating '\\0' .

 char buffer[] = 'e','a','b','g','e','l','e','g','o','n','\000','p','k','n','m','l','\000','j', 'i', 'h'};
 char p2[] = "eabgelegon\000pknml\000jih"; // matches
 //char p2[] = "eabgelegon\000pknml\000ji "; // fails
 //char p2[] = "eabgelegon\000pk ml\000jih"; // fails

 size_t buffer_size = sizeof(buffer);
 size_t p2_size = sizeof(p2);
 // p2 should be 1 longer than buffer due to its terminating \0
 if (buffer_size + 1 != p2_size) return NOT_EQUAL; 

 size_t i;
 for (i=0; i<buffer_size; i++) {
   if (buffer[i] != p2[i]) return NOT_EQUAL;
 }
 return EQUAL;

Note: sizeof() is not a function in C, but an operator.

If you don't recognize it:

char buffer[] = {'a', '\0'};
char p2[] = "a";

buffer and p2 are exactly the same. With buffer you declare a character array and assign the first character as 'a' and then an explicit null-terminating character '\\0' giving you a string "a" . With p2 you assign the string literal "a" and the null-terminating character is automatically appended to the end. Either way -- you get the same thing, buffer and p2 hold the string "a" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM