There are three structures; arrays a
and b
and pointer c
:
c --------------------------.
|
V
___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___ ___
a --> | a | \0| \0| \0| \0| b | i | g | \0| \0| r | i | d | e | \0|
´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´
___ ___ ___ ___ ___ ___ ___
b --> | F | l | y | i | n | g | \0|
´´´ ´´´ ´´´ ´´´ ´´´ ´´´ ´´´
This is the code:
int main(){
char a[3][5]={"a", "big", "ride"};
char b[]="Flying";
char *c=*(a+1);
puts(b+(c-*a)-2);
return 0;
}
Now what I can't understand is the expression b+(c-*a)-2
. Can someone be kind and break it down?
b+(c-*a)-2
is the same as &b[(c-*a)-2]
. In other words, if (c-*a)-2
is an offset into string b
, puts(b+(c-*a)-2)
would print the string b
starting from the position at the (c-*a)-2
offset.c
is assigned *(a+1)
, or *(&(a[1])), or simply a[1]
, which points to "big"
c-*a
(it is 5)b[5-2]
is "ing"
Disclaimer: if anyone tried to check in such code at my company, he would no longer be working for us.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With