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

Showing Answers 1 - 40 of 40 Answers

swaant

  • Feb 19th, 2006
 

ans is 1

  Was this answer useful?  Yes

Prabhu.T.S

  • Sep 1st, 2006
 

Declaration syntax error in fn. main()

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

  Was this answer useful?  Yes

bhoji_005

  • 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....... 

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

divya_419

  • Jun 27th, 2010
 

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

  Was this answer useful?  Yes

mahamd

  • 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]

  Was this answer useful?  Yes

  • May 1st, 2012
 

6

  Was this answer useful?  Yes

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 i−n-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 overflow; otherwise, the
behavior is undefined. 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."

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions