-2

I came to this problem while preparing to the exam this will print "different" my question is why ?

#include <stdio.h>
int main(void)
 {
 char t1[] = "abc";
 char *t2 = "abc";
 if (t1 == t2)
 printf("same");
 else
 printf("different");
 return 0;
 }

2 Answers2

2

Here

char t1[] = "abc";

t1 is character array and its name itself address i.e t1 and string literal "abc" both have same base address & both resides in stack section of RAM. It looks like

 0x100 0x101 0x102 0x103    <-- lets assume base address of t1 is 0x100
 ------------------------
|  a  |  b  |  c  |  \0  |  
 ------------------------
0x100
|
t1

And here

 char *t2 = "abc";

t2 is a character pointer, its itself resides in stack section but it points to string literal "abc" which presents in code section(read only) of RAM. It looks like

 0x300 0x301 0x302 0x303    <-- lets assume string literal "abc" stored at 0x300 location
 ------------------------
|  a  |  b  |  c  |  \0  |  
 ------------------------
 ---------
|  0x300  | ---> t2 points to different memory location
 ---------
0x200     ---> memory address allocated for t2
 |
 t2

Now when you do

if (t1 == t2) { } /* 0x100 == 0x300 --> false */

you are comparing two addresses i.e 0x100 and 0x300(in real time operating system assigns some real address not like 0x100) which is not same, hence the result "different".

But both location contents are same, so you should be using strcmp() to compare them. For e.g

if (strcmp(t1, t2) == 0) { }

However, if t1 and t2 are of same char* type. For e.g

char *t1 = "abc";
char *t2 = "abc";

in that case the both t1 and t2 points to same string literal & compiler will not be storing string literal "abc" to two different locations. Hence when you do

if(t1 == t2) { }

it results in same as t1 and t2 both points to same memory location.

Achal
  • 11,821
  • 2
  • 15
  • 37
-2

line 1: Allocates 4 bytes of memory space. The first three bytes are "a", "b" and "c". The fourth one is \0. Assigns the first memory address to t1.

line 2: When compiling the second line, the compiler has no information about the string defined in the first line being exactly the same as the second line; so, the compiler allocates a different (new) memory space, 4 bytes long, and the first three bytes are "a", "b" and "c". The fourth one is \0. Assigns the first memory address to t2.

It's clear that t1 is a char array pointer that points to a memory address and t2 is another char array pointer that points to another memory address. Clearly, t1 is not equal to t2, for they point to different memory addresses; though the two memory locations contain exactly same data.

ssd
  • 2,340
  • 5
  • 19
  • 37
  • 1
    (a) It is false that the compiler or program assigns the address of the first character to `t1`. `t1` is an array, not a pointer, so addresses cannot be assigned to it. What actually happens is storage is reserved for `t1` and the characters are copied into it (or written into it by some means). – Eric Postpischil Aug 10 '19 at 23:40
  • 1
    (b) It is false that the compiler has no information about the string defined in the first line being exactly the same. The compiler has the source code, and the source code contains that information. A simple comparison will reveal they are the same. In fact, using Apple LLVM c10.0.1 with clang-1001.0.46.4, I changed the code to `char *t1 = "abc"; char *t2 = "abc";` and printed `t1` and `t2`. They had the same address, thus showing the compiler did know the strings were the same. – Eric Postpischil Aug 10 '19 at 23:41
  • @EricPostpischil : I think your both comments are true. Though, the Apple clang case is an extra intelligence added to "that" compiler; a very specific talent that comes with object oriented version of "C". – ssd Aug 11 '19 at 12:21