Interview Question






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

first one is constant pointer to non-constant data
seccond one is non-constant pointer to constant data

- Anonymous June 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the first one, 3 bytes (including '\0') is allocated for 'ch' in stack, and value "ab" is stored in that.
In the second one, no memory is allocated for 'ch', it is a pointer pointing to the string constant "cd".

- csk June 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is no difference. Both are pointing to constant strings. Both are free to point to any other string of any length ... Of course, in the first one you have already declared that ch is an array of character types, whose length is fixed whereas in the second one, one can reassign it a character array of any length

- Manish June 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

incorrect. first one cannot point to any other string. it has memory reserved. one can only change the contents of that string which can be done by changing the elements of that array.

the second one can point to any other string, as its just a non-constant pointer.

first one is actually a constant pointer to the memory area reserved by the array. and the array is not constant

- AK July 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect .....
You are right AK ...

I also found same in many books .. good job !

- Aditya Kuamr Verma September 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i agree with AK
in my opinion first one is like saying char * const ch="Ab";
And csk too that u cant expand the memory pointed by it now.

- unicorn August 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Given the following:

char A[] = "ab";
char* B  = "cd;

A is of type char[3] and B is of type char*. They are not equivalent types, but they are very similar and most compilers allow you to interchange them in many contexts, but not all. For example;

B = A;    // Implicit conversion of char[3] to char*, OK
strlen(A) // Same implicit conversion 
A = B;    // ERROR cannot convert from 
          //  'char*' to 'char[3]'

Reference "The C++ Progamming Language, Special Edition By Bjarne Stroustrup" pp 91-92 section 5.3

- Developer October 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

apart from the pointer type differences , the first allocates memry on the stack while the second could allocate memory on bss because the 2nd type's string is constant.

- raja August 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

try to build the solid foundation of the bricks that others have taken, life will not be so hard.

- Pullan.Linda January 05, 2020 | 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