Function syntax

String abc = showData()["Hello"];
can somebody explain what is use of ["Hello"] after function call?

Questions by ak.nextptr   answers by ak.nextptr

Showing Answers 1 - 6 of 6 Answers

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.  

  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