0
I didn't understand the syntax itself. Please trace this program.
#include <stdio.h> int main() { char str[] = "%d %c", arr[] = "Sololearn"; printf(str, 0[arr], 2[arr + 3]); return 0; }
3 ответов
+ 3
The key here is to understand that
a[n] is equivalent to *(a+n)
This means that doing a[0] and 0[a] yields the same results. In the print statement, we see two of such instances which may be peculiar at first glance, but rearranging how it is written, we get
printf(str, arr[0], arr[2+3]);
so it isn't as hard to deduce the output now. Remember to consider the formatted string "%d %c", which causes the first argument to be printed as an integer (in this case, the ASCII value of arr[0]), and the second argument to be printed as a character.
+ 2
WOLFⓂ️ASTER Somewhat yes, but [arr+2] isn't syntactically accurate. There is a reason why I placed it in parentheses with an asterisk next to it (should be prior to the parentheses, I made a typo) was to "dereference" the pointer.
http://www.cplusplus.com/doc/tutorial/pointers/
When you invoke an array using its name, e.g. arr, this returns a pointer which points to the first element in the array. We access the value of that element using the deferencing operator, e.g.
int arr[5] = {1,2,3,4,5};
std::cout << arr; // prints address of first element in arr
std::cout << *arr; // prints 1
You'll see the equivalence of that to doing arr[0], since arr[0] is equal to *(arr+0), hence *arr. Observe:
std::cout << arr[1]; // prints 2
std::cout << *(arr+1); // also prints 2
std::cout << [arr+1]; // syntax error
0
so arr[2] = 2[arr] = [arr +2] right ? Hatsy Rei
Why did you write a+n in parentheses and use * after that in your answer above ?