Interview Question


Country: United States




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

sizeof (p) is showing the size of char *p , you can check , do not initialise it , still it will show sizeof(p) as 4 and the sizeof("abcd") is showing the size of string abcd + null character.

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

By the way, the correct way to get the length of the string would be

char p2[]="abcd";
sizeof(p2);

In the original example, p decays to a pointer, so sizeof(p) gives the size of the pointer; whereas p2 doesn't decay to a pointer and gives the size of the string.

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

sizeof(p) is the size of the pointer. It's not necessarily 4. On 64 bit machines, it'll be 8.

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

and sizeof(*p) is 1.
for sizeof, *p will point to the first character, and not the string.

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

Since the sizeof(p)=4 , whereas sizeof("abcd")=5
Explain it .

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

Because:
1).p stores address of something.A variable that stores address of something(integer,char,etc etc) is a pointer and has size =4;i.e sizeof(p)=4;(or 8 on 64 bit machines)
2)"abcd"is still a null terminated string(array of characters) in memory.hence has size 5;

- I want to code July 05, 2012 | Flag


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