Bloomberg LP Interview Question for Software Engineer / Developers






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

const pointer:

char const * p;
const char * p;

both are same. p can point anywhere u want, but, will not be allowed to modify any data using this pointer.
p[1] = 'a'; //error

pointer to const:
char a = 'a';
char * const p = &a;
p can't point anywhere else, but can modify a's data.
char b = 'b';
p = &b; //err

BONUS: const pointer to const data.
char const * const p = &a;

Effective C++ by Scott Meyers, Item 21: Use const whenever possible.

- Anonymous November 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are wrong.

const pointer:

char * const p;

const data (pointer to const):
const char *p; // or
char const *p;

- redroof November 05, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nopes , the anonymus guy is correct !!

- Sid November 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think most of the anonymous guy is correct.
But in my understanding,
const pointer is char * const p (pointer cannot point elsewhere after initialized)
pointer to const is const char * p (data cannot be changed).

- Edie November 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The anonymous guy is not correct...
A const pointer is defined as "int * const ptr"..which means it can be made to point to an integer only once...and cannot be redirected to another...
Pointer to a const is declared as "int const * ptr"..which means it points to const integer and the pointer cannot be used to modify the value of integer it is pointing to

- Abhi November 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The anonymous guy is not correct...
A const pointer is defined as "int * const ptr"..which means it can be made to point to an integer only once...and cannot be redirected to another...
Pointer to a const is declared as "int const * ptr"..which means it points to const integer and the pointer cannot be used to modify the value of integer it is pointing to

- Abhi November 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thats precisely what @anonymous has said

- CodeRed October 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

- const Fred* p means "p points to a Fred that is const" — that is, the Fred object can't be changed via p.
- Fred* const p means "p is a const pointer to a Fred" — that is, you can change the Fred object via p, but you can't change the pointer p itself.

- Anonymous2 January 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct answer:

char a='a',b='b';
const char * ptr=&a; // Pointer to const. *(ptr)='b'; //Invalid
char * const ptr=&a; // Const Pointer . ptr=&b ; //Invalid

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

Now consider the following three declarations assuming that char_A has been defined as a type char variable.:

const char * myPtr = &char_A;
char * const myPtr = &char_A;
const char * const myPtr = &char_A;

What is the difference between each of the valid ones? Do you know?

They are all three valid and correct declarations. Each assigns the addres of char_A to a character pointer. The difference is in what is constant.

The first declaration:

const char * myPtr

declares a pointer to a constant character. You cannot use this pointer to change the value being pointed to:

char char_A = 'A';
const char * myPtr = &char_A;
*myPtr = 'J'; // error - can't change value of *myPtr

The second declaration,

char * const myPtr

declares a constant pointer to a character. The location stored in the pointer cannot change. You cannot change where this pointer points:

char char_A = 'A';
char char_B = 'B';

char * const myPtr = &char_A;
myPtr = &char_B; // error - can't change address of myPtr

The third declares a pointer to a character where both the pointer value and the value being pointed at will not change.

- divyaC May 30, 2010 | 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