Home > Back-end >  const char* and char[] (char array)
const char* and char[] (char array)

Time:02-19

int main(){
  const char *a[] = {"zero","one"};
  char b[] = "zero";
  bool c = b==a[0];
}

Why is bool value zero since I passed the same string and how to make it as 1?

CodePudding user response:

const char *a[] = {"zero","one"}; creates two arrays, one filled with the characters of “zero” followed by a null character, and the other filled with the characters of “one” followed by a null character, and sets a[0] to point to the first element of the first array and a[1] to point to the first element of the second array.

char b[] = "zero"; creates an array and fills it with the characters of “zero” followed by a null character.

b==a[0] converts the array b to a pointer to its first element and compares that pointer to the pointer a[0]. Since these point to different arrays, the == operation evaluates as false (zero). The fact the two arrays contain the same data is irrelevant. The pointers are compared, not the array contents.

To compare two strings, you can use std::strcmp, declared in the header <cstring>. std::strcmp(b, a[0]) returns zero if and only if the two strings are the same. bool c = std::strcmp(b, a[0]) == 0; will set c to true if and only if the two strings are the same.

(std::strcmp returns a negative number if the first string is “less than” the second string, where “less than” means the first position that differs has a lower character code in the first string than in the second string, taking the characters as unsigned char values. It returns a positive number if the first string is greater than the second string.)

CodePudding user response:

When you call == on char* or any other pointers, in C , you test whether the two pointers are pointing to the same object.

a[0] and b are pointing to different characters. So, the result will be false.

If you want to compare the content, you need to use strcmp or, better, use std::string and use == on them.

P.S.: I skipped over the part where the char array decays into a pointer. You can come back to this concept later.

CodePudding user response:

The thing is, what you are doing is not comparing two strings, but comparing two addresses. When you are comparing b with a[0], it starts comparing their addresses, address meaning the physical address where the value is residing in memory which are obviously different, that is why it returns 0.

what you need is strcmp function, it takes 2 character pointers and returns whether the strings themselves are same/equal or not.

CodePudding user response:

When you declare char b[] = "zero" what you are doing is assigning b with the location in memory where list list of characters begins.

When you declare char *a[] = {"zero","one"};, you are creating a list of memory locations, and assigning the location of that list to a.

When they are compared, they are different points in memory, because they are different pointers to different lists.

If you want to compare "strings" then you can import string function or use strcmp function.

  •  Tags:  
  • c
  • Related