473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Please help with an interview question

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

Jul 22 '05 #1
9 1324
* so***********@yahoo.com:

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
so***********@yahoo.com wrote:
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
Jul 22 '05 #3

<so***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Jul 22 '05 #4
so***********@yahoo.com wrote:
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.
Jul 22 '05 #5
Mike Wahler wrote:
<so***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Jul 22 '05 #6
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:zH*******************@newsread1.mlpsca01.us.t o.verio.net...
Mike Wahler wrote:
<so***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Jul 22 '05 #7
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:WN*****************@newsread1.news.pas.earthl ink.net...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:zH*******************@newsread1.mlpsca01.us.t o.verio.net...
Mike Wahler wrote:
>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
Jul 22 '05 #8
Victor Bazarov wrote:
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.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #9
Victor Bazarov wrote:
Mike Wahler wrote:
Something that calls itself soup_or_power wrote:
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
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: free2cric | last post by:
Hi, I attanded an interview on C++ Question asked were and my answers to them.. 1. In a CPP program what does memory leak occure? -- i said.. In a constructor , the pointer variables were...
26
by: ROSY | last post by:
hello 2 all, where can i get interview question on C & C++. plz give me links it will be so helpful. bye
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
0
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
5
by: amey1.kamat | last post by:
I got this in a company paper I was solving online... Can someone explain me what this code should do?and whats the error? int func (int (*)(int) , int); int cube(int n) { return (n*n*n); }...
0
by: freesoftwarepdfs | last post by:
Ultimate list of Interview question website.....Do not miss it http://www.questpond.com http://msdotnetsupport.blogspot.com/2007/01/net-interview-questions-by-dutt-part-2.html...
0
by: Free PDF | last post by:
..NET , SQL Server interview questions websites.... http://www.questpond.com http://www.geocities.com/dotnetinterviews/...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.