R
R
re3r02021-06-25 17:38:50
C++ / C#
re3r0, 2021-06-25 17:38:50

Why is the Character Array not equal to a normal string?

char var[] = {'a', 'b', 'c', '\0'};
char var2[] = "abc";

if (var == var2)
{
  printf("%s\n", "all is ok !"); 
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-06-25
@re3r0

Because your code is comparing pointers (memory addresses for strings in var and var2).
To compare strings, there are the strcmp functions (and its variations).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question