Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
2
of 2 vote

Yes these two pointers are equal , check the address of pointers with &p value after typecasting char pointer to void *...

- kk July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry not with &p , instead %p format specifier .

- kk July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think these days compiler does the optimization while compiling.

#include <iostream>
int main()
{
        char * p="i am the one";
        char * q="i am the one";

        if (p==q)
                std::cout << "Pointer has same address";
        else
                std::cout << "Pointer has same Different address";
}

result is "Pointer has same address".

- Munish Arya July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no they are not equal
just compare & p and &q

- Anonymous July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Of course &p and &q are different. But p and q are the same, and that's what the question asks.

- Anonymous July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

No they are not equal. The content at the location are equal but as such they are actually two distinct strings in different memory locations.

- Anonymous July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

so , that means statement ------> if(p==c)
returns false ??? or
p==c ----->compares the string ???

- Shobhit July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ya. the p==c compares the values at the location itself.
however when you print &p and &c you will notice that the values are different indicating that their addresses are different

- Anonymous July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

when you do &p and &c ...it 's just the values of these variable p & q , not the address they are pointing to ..... what i think is both pointer's are holding same address . whenever string literals assigned to pointers ...it's stored in a read only memory of data segment and nobody can change this value.. so the same string assigned to other pointer store the same address.

- kk July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

they are nt equal

- supriya July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

They can be equal because the compiler might decide to have the pointers point to the same location as an optimization. I would never rely on that kind of behavior, though.

- Anonymous July 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

"havfun" is static resource.
so, two pointers have same value.

- und3r July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but, it's depends on compiler, exactly.

- und3r July 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well the pointers are equal because they both are pointing to the same string. The above code can also be written as
char *p = "hello";
char * q = p;
Now this is clear that both p and q are "equal". But they both are at different memory locations and there address would be different. This is because the same memory location cannot be allocated to two members.

- Spock July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C,C++,JAVA, strings are immutable (not d array of characters). This means a string once created cannot be modified. Only flushing d buffer can remove it. Next point is when a string is created it is stored in buffer. Next time again when new string is created, it checked whether it is present in buffer or not?? if present tht address is assigned, otherwise new address stores new string n this new address is assigned. Hope tht helps

- SHAN July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

p and q will be same, because if we look at a process's memory, there is an area where all the global variables and constants are stored for that process, and "havfun" falls in this category, therefore p and q are having value.

- Anonymous July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

P and q are two different pointers pointing at two different locations and these locations have the same value.

Because p and q are not initialized the memory locations pointed by p and q might the same. Clearly this code is not safe.

- N568 August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes address will be the same, because both the character pointer are pointer to the string which is immutable, hence for optimization purposes stored in String Pool.

- ashi August 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String literals are stored in the read-only area of the data segment. So it makes no sense to make 2 different copies of the same string, as u cannot edit them anyway. Hence, these two pointers will point to the same location in memory.
You can always assign different values to the pointers, but you cannot change the value.
e.g. you cannot do p[2]='a';

- s August 19, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More