1 votes 1 votes #include <stdio.h> void f(char**); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char **p) { char *t; t = (p += sizeof(int))[-1]; printf("%s\n", t); } can anyone explain this program? Siddharth Bhardawaj asked May 3, 2018 Siddharth Bhardawaj 782 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 2 votes 2 votes $sizeof(int)$ is compiler dependent it can be $2$ or $4$ depending on the compiler if it is $2$, then $(p += sizeof(int))[-1]$ can be written as $(p += 2)[-1] $ which can be written as $(p = p+2 - 1)$ which returns address p+1 which is address of $2^{nd}$ element in $argv[]$. it gives $cd$ if it is $4$, then $(p += sizeof(int))[-1]$ can be written as $(p += 4)[-1] $ which can be written as $(p = p+4 - 1)$ which returns address p+3 which is address of $4^{th}$ element in $argv[]$ it will give $gh$ pankaj_vir answered May 3, 2018 • selected May 3, 2018 by Siddharth Bhardawaj pankaj_vir comment Share Follow See 1 comment See all 1 1 comment reply Siddharth Bhardawaj commented May 3, 2018 reply Follow Share Is my approach is correct? still i m not getting the clarity...please help 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes **p contains add of "ab" According to OperatorPrecedence 1) sizeof(int) = 4 2) (p +=sozeof(int)) == (p = p +4) and p+4 is address of "ij" 3) (p += sizeof(int))[-1] mean "gh" so t = gh arya_stark answered Jul 3, 2018 arya_stark comment Share Follow See all 0 reply Please log in or register to add a comment.