const char* equals char[] | Sololearn: Learn to code for FREE!


const char* equals char[]

Hi all, does anyone know where to put the "\0" to make const char* equal to char[]? If I run this code, one() returns 1 and zero() returns 0, which is kinda strange to me, but I think the "\0" should be able to fix that up somehow. Please let me know! #include <stdio.h> char one(), zero(); int main() { printf("One: %c\n", one()); printf("Zero: %c", zero()); } char one(){ //Outputs 1 unsigned long long int a = 0.0f; const char b = .0; return a == b ? '1' : '0'; } char zero(){ //Outputs 0 const char* p = "sololearn"; char q[] = "sololearn"; return q == p ? '1' : '0'; }

5/22/2020 10:58:21 AM

Orville Vroemen

3 Answers

New Answer


That's not the cause of the problem here, it's that you can't compare strings with ==. Have a look at the strcmp function in string.h: EDIT: with == you are comparing the pointers. The pointers point to different memory locations hence they aren't equal.


Thank you Schindlabua, I forgot to dereference the q and the p, *q == *p returns 1.


Orville Vroemen Careful though, that will only compare the first character!