GeekInterview.com
Series: Subject: Topic:
Question: 117 of 195

WIPRO Technical Interview

main() { int a[]={0 1 2 3}; int *b; b=&a[2]; printf("%d",b[-1]); } TECHNICAL INTERVIEW
This question is related to Wipro Interview
Asked by: Interview Candidate | Asked on: Jul 7th, 2005
Showing Answers 1 - 16 of 16 Answers
Suhas

Answered On : Nov 18th, 2005

View all answers by Suhas

3

  
Login to rate this answer.
swaant

Answered On : Feb 19th, 2006

ans is 1

  
Login to rate this answer.
purani86

Answered On : Jun 18th, 2006

View all answers by purani86

error.since array index cannot be negative

  
Login to rate this answer.
Prabhu.T.S

Answered On : Sep 1st, 2006

Declaration syntax error in fn. main()

Elements in the array should be seperated by comma while declaration.

  
Login to rate this answer.
bhoji_005

Answered On : Oct 5th, 2007

main()
{
int a[]={0 1 2 3};
int *b;
b=&a[2];
printf("%d",b[-1]);
}
the ans is :error
why because of ,b[-1] indicates subscript of -1.....
there is no negetive subscrpts....... 

  
Login to rate this answer.

Its a fact that the array index cannot be negative but I ran the program on both Linux compilerand turbo c for both of which I got '1' as the output

can anyone interpret this and explain me the concept plzzzzzzzzz

  
Login to rate this answer.
divinegeek

Answered On : Mar 30th, 2008

View all answers by divinegeek

Answer will be 1.........
because a[2]=2
and when it will calculate b[-1] answer is 1............

  
Login to rate this answer.

It's logical and easy. See we have a[] i.e. assigned with a set of values. Now,a pointer b is been referred to the base address of 2nd position of the array's location i.e. the pointer b is a pointer of arrays which store the second location of array as its first location in the pointer's base address i.e. b[0] contains a[2] value as its value thus, the previous value is been stored as its b[-1] value.

So, frnd ans is thus, : 1

  
Login to rate this answer.
divya_419

Answered On : Jun 27th, 2010

View all answers by divya_419

Answer is 2,bcoz b[-1] gives difference of bytes related to address.as b=&a[2];b[-1] indicates one element before and the answer is 2 for gcc

  
Login to rate this answer.
srikanth kala

Answered On : Dec 6th, 2011

View all answers by srikanth kala

The answer is : Error
Why because of b[-1] indicates subscript of -1.
There is no negetive subscripts.

  
Login to rate this answer.
mahamd

Answered On : Apr 15th, 2012

It will give answer 1 because when we give b=&a[2] at that time b take the reference of a[2] and get value 2.
now for b itself works as array and starting its subscript from b[0]=2
now whenever we gives b[-1] means it will give the value after taking resource from a[] so,in b[-1] maens
a[0]=b[-2]
a[1]=b[-1]
a[2]=b[0]
a[3]=b[2]

  
Login to rate this answer.
dhirajsharma

Answered On : Apr 25th, 2012

View all answers by dhirajsharma

The answer is 1. Negative subscripts are definitely allowed in C and C++.

  
Login to rate this answer.

Answered On : May 1st, 2012

6

  
Login to rate this answer.
jbode

Answered On : Jun 29th, 2012

View all answers by jbode

The result will indeed be 1 (a[2] is the third element of the array, so b[-1] will be equivalent to a[1]). There is no restriction against using negative array indices. From the latest online draft of the C language standard (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), section 6.5.6, paragraph 8:

"When an expression that has integer type is added to or subtracted from a pointer, the
result has the type of the pointer operand. If the pointer operand points to an element of
an array object, and the array is large enough, the result points to an element offset from
the original element such that the difference of the subscripts of the resulting and original
array elements equals the integer expression. In other words, if the expression P points to
the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and
(P)-N (where N has the value n) point to, respectively, the i+n-th and in-th elements of
the array object, provided they exist. Moreover, if the expression P points to the last
element of an array object, the expression (P)+1 points one past the last element of the
array object, and if the expression Q points one past the last element of an array object,
the expression (Q)-1 points to the last element of the array object. If both the pointer
operand and the result point to elements of the same array object, or one past the last
element of the array object, the evaluation shall not produce an overow; otherwise, the
behavior is undened. If the result points one past the last element of the array object, it
shall not be used as the operand of a unary * operator that is evaluated."

  
Login to rate this answer.
nawinbalu

Answered On : Oct 11th, 2012

View all answers by nawinbalu

negative subscripts are allowed but the declaration of the array is not right there should be comas between them

  
Login to rate this answer.
Munna541

Answered On : Oct 27th, 2012

View all answers by Munna541

Subscript operators always takes negative number as positive. so ans is 1

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

C Interview Questions

C++ Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.