0

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.

dgamma3
  • 2,333
  • 4
  • 26
  • 47
  • 5
    Use `strcmp` or `strncmp`. – haccks Aug 27 '14 at 13:25
  • 1
    `buffer` and `p2`, in your `printf()`, are taken as **different** addresses, namely the addresses of each array first element. – pmg Aug 27 '14 at 13:26
  • 2
    Note that `char buffer[] = {'e','a','b','g','e','l','e','g','o','n','\000','p','k','n','m','l','\000','j', 'i', 'h'};` defines a string with extra data after the `'\000'`. Using string functions on such an array will likely yield unexpected results. Rather than `strcmp()` you will be better off using `memcmp()`. – pmg Aug 27 '14 at 13:31
  • you are comparing the address. haccks says the ansqwer. – Jason Hu Aug 27 '14 at 13:37
  • 2
    With `char buffer[] = {'a', '0'};`, `buffer` is not a C string at it has no terminating `'\0'`. – chux - Reinstate Monica Aug 27 '14 at 15:35

3 Answers3

1

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);
pmg
  • 106,608
  • 13
  • 126
  • 198
1

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

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".

rubenvb
  • 74,642
  • 33
  • 187
  • 332
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • OP has `char buffer[] = {'a', '0'};` and this answer has `char buffer[] = {'a', '\0'};`. OP's `buffer` is not a C string. this answer's `buffer` is a string. – chux - Reinstate Monica Aug 27 '14 at 15:33
  • Good catch. I'll amend. (the question has been greatly extended since the answer was posted) Not only extended -- **it has been changed** The specification of the problem above was a **copy** or the original question. Obviously, if you change the question, then the answer to the original question, will no longer apply as written. – David C. Rankin Aug 27 '14 at 15:35
  • @chux -- just so we are clear. The question as originally posted was `char buffer[] = {'a', '\0'};` The question was changed after this answer was posted (and I suspect will be changed again as `{'a', '0'}` makes little sense and strcmp would puke at a non-null-terminated string) (strcmp has also been removed apparently) – David C. Rankin Aug 27 '14 at 15:42