Skip to main content
make clear the result of strcmp
Source Link
pmg
  • 109.3k
  • 14
  • 132
  • 203

In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* ...name is not "foobar" */;

In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* ... */;

In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* name is not "foobar" */;
deleted 88 characters in body
Source Link
pmg
  • 109.3k
  • 14
  • 132
  • 203

Rahhhhhhhhhhhhhhhhhhh, tags changed after I wrote this answer. I don't know C++.

In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* ... */;

Rahhhhhhhhhhhhhhhhhhh, tags changed after I wrote this answer. I don't know C++.

In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* ... */;

In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* ... */;
Not C++, I answered C
Source Link
pmg
  • 109.3k
  • 14
  • 132
  • 203

BecauseRahhhhhhhhhhhhhhhhhhh, tags changed after I wrote this answer. I don't know C++.

In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* ... */;

Because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* ... */;

Rahhhhhhhhhhhhhhhhhhh, tags changed after I wrote this answer. I don't know C++.

In C because, in most contexts, an array "decays into a pointer to its first element".

So, when you have the array "foobar" and use it in most contexts, it decays into a pointer:

if (name == "foobar") /* ... */; /* comparing name with a pointer */

What you want it to compare the contents of the array with something. You can do that manually

if ('p' == *("foobar")) /* ... */; /* false: 'p' != 'f' */
if ('m' == *("foobar"+1)) /* ... */; /* false: 'm' != 'o' */
if ('g' == *("foobar"+2)) /* ... */; /* false: 'g' != 'o' */

or automatically

if (strcmp(name, "foobar")) /* ... */;
Source Link
pmg
  • 109.3k
  • 14
  • 132
  • 203
Loading