Hello all, recently I gave my C++ exam, I came across this question. I was unable to understand the logic.
Can anyone explain what happens inside foo() ?
Thanks for your help :)
#include <iostream>
using namespace std;
void foo(char **p);
int main()
{
char *argv[] = {"ab","cd","ef","gh","ij","kl"};
foo(argv);
return 0;
}
void foo(char **p)
{
char *t = (p += sizeof(int))[-1];
cout << t << '\n';
}
sizeof(int) == 4
for all modern systems.p[i]
equals to*(p + i)
. BTW,i[p]
is also correct syntax.Also, C++11 standart does not allow conversion from string literals (like
"ab"
in this code) tochar *
type. Tell it to your examiner.