GeekInterview.com
Series: Subject: Topic:
Question: 14 of 587

Function syntax

string abc = showData()["Hello"]; can somebody explain what is use of ["Hello"] after function call?
Asked by: ak.nextptr | Member Since Jan-2012 | Asked on: Jul 31st, 2012

View all questions by ak.nextptr   View all answers by ak.nextptr

Showing Answers 1 - 1 of 1 Answers
jbode

Answered On : Aug 6th, 2012

View all answers by jbode

C doesn't have a String data type, unless that supposed to be a typedef for something. Having said that, I think this is taking advantage of the fact that array indexing in C is commutative; IOW, "a[i]" and "i[a]" both evaluate to the ith element of the array a. If "showdata" returns an integral type, it can be used to index into the array expression "hello". Ive attached a quick-n-dirty example that illustrates this.

Code
  1. #include <stdio.h>
  2.  
  3. int foo(void)
  4. {
  5.   return 1;
  6. }
  7.  
  8. int main(void)
  9. {
  10.   printf("%c
  11. ", foo()["Hello"]);
  12.   printf("%c
  13. ", "Hello"[foo()]);
  14.   return 0;
  15. }
  16.  

  
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

Interview Question

 Ask Interview Question?

 

Latest Questions

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.