Please help with an interview question

S

soup_or_power

Sorry these are not c++ questions per se. Your help is appreciated.

Suppose arr is an array (e.g. int arr[100]) Here are some questions:
a)What does arr point to?
My answer: to the first element of the array i.e. arr[0]
b) What if we do a++ or --a?
a++ will not point to a[1]
--a will cause segmentation fault
c) Similarly if there is a pointer to arr (e.g. int * ptr=arr) what
does ptr++ do?
Don't know
 
A

Alf P. Steinbach

* (e-mail address removed):
Suppose arr is an array (e.g. int arr[100]) Here are some questions:
b) What if we do a++ or --a?

A conforming compiler should issue a diagnostic since it's not
a built-in C++ operation and cannot be defined by the user.
 
V

Victor Bazarov

Suppose arr is an array (e.g. int arr[100]) Here are some questions:
a)What does arr point to?
My answer: to the first element of the array i.e. arr[0]
Yep

c) Similarly if there is a pointer to arr (e.g. int * ptr=arr)

It's not "a pointer to arr". It's a pointer to int, which is simply
initialised with 'arr' expression.
what
does ptr++ do?
Don't know

Expression ptr++ has the _value_ of the original ptr and at the next
sequence point ptr points to the next element of the array (i.e. to
arr[1]). So, if your array begins with { 1, 2, 3 }, here is the code
fragment that illustrates that:

int arr[100] = { 1,2,3 };
int *ptr = arr; // ptr points to arr[0]
int a = *ptr; // 'a' gets value 1, ptr still points to arr[0]
int b = *ptr++; // 'b' gets value 1, ptr gets moved to arr[1]
int c = *ptr; // 'c' gets value 2

V
 
E

E. Robert Tisdale

Sorry these are not c++ questions per se. Your help is appreciated.

Suppose arr is an array (e.g. int arr[100]) Here are some questions:
a)What does arr point to?
My answer: to the first element of the array i.e. arr[0]

It was a trick question.
arr is the name of an array and *not* a pointer.
b) What if we do a++ or --a?
a++ will not point to a[1]
--a will cause segmentation fault

You can't increment or decrement an array name.
cat main.c
int main(int argc, char* argv[]) {
int arr[100];
arr++;
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
main.c: In function `main':
main.c:3: error: wrong type argument to increment
c) Similarly if there is a pointer to arr (e.g. int * ptr=arr) what
does ptr++ do?
Don't know.

ptr actually is a pointer that you can increment.
 
M

Mike Wahler

Sorry these are not c++ questions per se. Your help is appreciated.

Suppose arr is an array (e.g. int arr[100]) Here are some questions:
a)What does arr point to?

It doesn't point to anything at all. It's not
a pointer, it's an array. An array is not a pointer.
A pointer is not an array.
My answer: to the first element of the array i.e. arr[0]
Wrong.

b) What if we do a++ or --a?

You will get a diagnostic from a conforming compiler.
a++ will not point to a[1]
--a will cause segmentation fault

Neither should compile at all. So those answers
don't really mean anything.
c) Similarly if there is a pointer to arr (e.g. int * ptr=arr)

That's not a pointer to an array, but a pointer to a (single)
type 'int' object. A pointer to the above array would be
defined as:

int (*p)[100];
what
does ptr++ do?

As defined by you above (int *ptr), it points to a memory
address whose value is sizeof(int) bytes greater than the
address it previously stored (before the increment).
Don't know

It appears to me that the interviewer doesn't know C++
(which is very common in my experience).

-Mike
 
V

Victor Bazarov

Mike said:
Sorry these are not c++ questions per se. Your help is appreciated.

Suppose arr is an array (e.g. int arr[100]) Here are some questions:
a)What does arr point to?


It doesn't point to anything at all. It's not
a pointer, it's an array. An array is not a pointer.
A pointer is not an array.

'arr' is also an expression. The name of an array if used in
an expression decays to a pointer to the first element.
My answer: to the first element of the array i.e. arr[0]


Wrong.

Not wrong. It's just one answer out of several which are all acceptable.

V
 
M

Mike Wahler

Victor Bazarov said:
Mike said:
Sorry these are not c++ questions per se. Your help is appreciated.

Suppose arr is an array (e.g. int arr[100]) Here are some questions:
a)What does arr point to?


It doesn't point to anything at all. It's not
a pointer, it's an array. An array is not a pointer.
A pointer is not an array.

'arr' is also an expression. The name of an array if used in
an expression

I like Torek's term "value context", which excludes
'sizeof(arr)' and '&arr', which imo qualify
as 'expressions'.
decays to a pointer to the first element.

Yes, I'm aware of that, but I read the question literally:

"Suppose arr is an array (e.g. int arr[100]). What does arr
point to".

I say:
arr is an array.
An array does not point.

Without further given context, I didn't want to make any
assumptions (e.g. 'as an expression').
My answer: to the first element of the array i.e. arr[0]


Wrong.

Not wrong. It's just one answer out of several which are all acceptable.

Eye of the beholder and all that. :)


-Mike
 
M

Mike Wahler

Mike Wahler said:
Victor Bazarov said:
Mike said:
My answer: to the first element of the array i.e. arr[0]


Wrong.

Not wrong. It's just one answer out of several which are all
acceptable.

Eye of the beholder and all that. :)

I also support my suspicion that someone thinks an array
is a pointer with the appearance of the other questions
about a++ and --a (I assume :)-)) that 'a' was a typo,
intended to be 'arr')

-Mike
 
E

E. Robert Tisdale

Victor said:
Mike said:
Something said:
Sorry these are not c++ questions per se. Your help is appreciated.

Suppose arr is an array (e.g. int arr[100]) Here are some questions:
a)What does arr point to?

It doesn't point to anything at all.
It's not a pointer, it's an array.
An array is not a pointer.
A pointer is not an array.

'arr' is also an expression.
The name of an array, if used in an expression,
decays to a pointer to the first element.
My answer: to the first element of the array i.e. arr[0]

Wrong.

Not wrong.
It's just one answer out of several which are all acceptable.
cat main.cc
int main(int argc, char* argv[]) {
int arr[100];
arr++;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)':
main.cc:3: error: cast to non-reference type used as lvalue
main.cc:3: error: ISO C++ forbids cast \
to non-reference type used as lvalue
main.cc:3: error: non-lvalue in assignment
 
A

Andrey Tarasevich

Victor said:
Sorry these are not c++ questions per se. Your help is appreciated.

Suppose arr is an array (e.g. int arr[100]) Here are some questions:
a)What does arr point to?


It doesn't point to anything at all. It's not
a pointer, it's an array. An array is not a pointer.
A pointer is not an array.

'arr' is also an expression. The name of an array if used in
an expression decays to a pointer to the first element.
...

Even if we assume that 'arr' is a full expression, it still depends on
the context. In an expression statement

arr;

it doesn't decay (see 6.2/1). In an initializer expression

int* p = arr;

it does.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top