441 views
2 votes
2 votes
if we write in C

char a[]=”TAJ”;

char b[]=”TAJ”;

and on comparing (a==b) false is returned

where as in char *p=”TAJ”;

char *q=”TAJ” it return TRUE

why??

1 Answer

0 votes
0 votes

In the First Case, you are comparing the address of the two array which will always be different. Here value of a = 1000 (address 1), b = 2000 (address 2)  and as we know they cannot be stored on the same address therefore, the result is false.

In the Second Case, you are making a string in a memory and you are pointing to that memory, like TAJ is stored in consecutive memory location suppose starting from address 1000, i.e. T → 1000 ( address ). this type of declaring is also called “string constant”.

So, in the Second Case, as we are using the same string, a & b both point to the same string present in the memory (compiler optimization). Therefore there pointing address is the same, making the result is true.

 NOTE: the variables a and b are present at different memory location, they just point to the same memory location.

Related questions

0 votes
0 votes
0 answers
1
Sagar475 asked Jan 28, 2022
167 views
If it’s written that some synchronization construct statisfies bounded waiting than it means the process is going to wait nearly forever fo it’s turn to execute?
0 votes
0 votes
0 answers
2
Sagar475 asked Jan 15, 2022
256 views
suppose we are given the congestion window 30KB and TO occurs than new th=15 and mss given =2 so when we start with slow start than sequence goes like:-2 → 4 → 8 → ...
0 votes
0 votes
0 answers
3
srestha asked Apr 27, 2019
293 views
How bottom-up parser like Operator-Precedence parser parse some ambiguous grammar?According to stanford diagram , ambiguous grammar cannot be parsed and it is separate fo...