Hesham Eraqi
Hesham Eraqi

Reputation: 2542

Comparing Char Pointers

I'm trying to compare two char pointers:

char * x;
char * y;
   
x = "Hesham";
y = "Hesham";
   
printf("%d %d \n", &x, &y);
   
if(x==y)
{
  printf("=\n");
}
else
{
  printf("!=\n");
}

The execution result is:

2293368 2293360
=
  1. How come the two pointers are of different addresses and the operation == returns true?

  2. Why didn't the compiler store the string literal Hesham just once and use its address twice for x and y?

Upvotes: 0

Views: 4902

Answers (4)

Lundin
Lundin

Reputation: 213398

There's an optimization technique called "string pooling" where the compiler may try to store identical string literals at the same memory location. This may or may not be active, depending on compiler and optimization settings.

Example:

#include <stdio.h>

int main()
{
  char* s1 = "hello";
  char* s2 = "hello";
  if(s1==s2)
    puts("same");
  else
    printf("%s is not %s", s1, s2);
}

Default output gcc x64 12.1:

same

Default output MSVC x64 19.32:

hello is not hello

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

It depends on a compiler option whether the compiler will store equal string literals as separate literals or as one literal/ Usually by default compilers store equal string literals as one string literal that to minimize the memory usage.

In these statements

   x = "Hesham";
   y = "Hesham";

you assign the same address of the first character of the string literal to variables x and y. As the compiler stores these two string literals as one string literals then it is obvious that x == ythat is x and y were assigned the same value.

However if you would set on the option of the compiler to force the compiler to store equal string literals as separate literals then the result of expression x == y would be equal to false. If you want to compare the string literals you have to write

if ( strcmp( x, y ) == 0 ) { /*...*/ }

Upvotes: 2

user3647351
user3647351

Reputation: 175

According to The C++ Programming Language by Bjarne Stroustrup, He said

Whether two identical string literals are allocated as one array or as two is
implementation-defined.

I think it also works for C given that C++ is based on C99. In your case, only one of them is allocated and is used twice, which means x and y points to the identical string.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612794

How come the two pointers are of different addresses and the operation == returns true?

&x and &y are the addresses of the pointer variables rather than the addresses of the string. Since x and y are different variables, they have different addresses.

You then compare the values of x and y which happen to be the same because the compiler has noticed that the two string literals have the same value and only stored a single copy of the literal.

Why didn't the compiler store the string literal "Hesham" just once and use its address twice for x and y?

It did. That's why x == y evaluates as true.


One other point to make is that you should use the %p format specifier when printing pointers.

Upvotes: 12

Related Questions