Interview Question


Country: United States




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

The way to understand pointer and const declarations is to read them right-to-left.

int const *i;

Pointer (*) to constant (const) int. It means you can reassign the pointer, e.g. i = &some_other_constant_int is valid, but you cannot alter the int through the pointer, because the int is constant, e.g. *i = 2 is invalid.

int *const i

Constant (const) pointer (*) to int. This means the pointer itself is constant, but the value it points to is not. So *i = 2 would be valid, but i = &some_other_int would not be.

const int *i

Pointer(*) to an int constant. This is the same as int const *i. Because of this and because the more natural way to be consistent with the right-to-left rule is to write int const * i, some programmers dislike const int* i. However, I would note that unsigned int * is still the way to declare a pointer to unsigned int, so to me it makes good sense to say that const int * is a pointer to const int. I just read "const int" as one term, just like I would consider "unsigned int" to be one term.


Now that we know this, how about a new one?

int const * const i

Easy, using the right-to-left rule. Constant (const) pointer (*) to constant (const) int. In other words, neither the pointer nor the value it points to can be modified. *i = 2 is invalid, and so is i = &some_other_const_int. Based on the previous discussion, you might correctly infer that "const int * const i" would mean the same thing.

- eugene.yarovoi September 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Const int *i and int const *i are same and represent constant value. You cannot change the value of the pointer
Int *const i represent consant pointer. You cannot change the pointer to which it. Is pointeting to,, you can change the value

- Dashdash September 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wat abt const * int i ??

- nani September 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

-1 for being unclear. And nani, const * int i is not valid syntax.

- eugene.yarovoi September 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The best way to understand this is ..
1. whatever comes to right of "const" .. become a constant.
2. so for eg. const int *i .. here *i ( deferenced value) is constant so you cannot change the VALUE the pointer points to..
..another eg. int *const i .. here i comes after the const keyword.. hence only the address that the pointer points to stays constant..i.e u can change the value pointed to by the pointer. .but you cannot make it point to any other mem loc.

- kasprov September 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C you declare the type of an expression, not a variable.

const int *i; -> *i is an int, that cannot be modified
int const *i; -> same as before, no difference
int *const i; -> i is an int* and the pointer itself cannot be modified - but the content can! e.g. *i = 10; is absolutely valid

const int * const i; -> neither of the pointer, nor the content can be modified. [This only makes sense with initializing i to an existing address]

- Selmeczy, Péter September 24, 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