edited by
440 views
2 votes
2 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);
}



(a) ab                                                                           (b) cd

(c) ef                                                                           (d) gh

edited by

1 Answer

1 votes
1 votes

  char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };

argv is a array of pointers.===> let this array stored at address 1000.

"ab" ----- string constant, stored at Read-only location ===> assume base address of that string is 100

"cd"  ----- string constant, stored at Read-only location ===> assume base address of that string is 200

"ef"  ----- string constant, stored at Read-only location ===> assume base address of that string is 300

"gh"  ----- string constant, stored at Read-only location ===> assume base address of that string is 400

"ij"  ----- string constant, stored at Read-only location ===> assume base address of that string is 500

"kl"  ----- string constant, stored at Read-only location ===> assume base address of that string is 600

∴ the pointer at 0th location of argv points to the "ab"  ===> argv[0]= 100

the pointer at 1st location of argv points to the "cd"  ===> argv[1]= 200

the pointer at 2nd location of argv points to the "ef"  ===> argv[2]= 300

the pointer at 3rd location of argv points to the "gh"  ===> argv[3]= 400

the pointer at 4th location of argv points to the "ij"  ===> argv[4]= 500

the pointer at 5th location of argv points to the "kl"  ===> argv[5]= 600

 

void f(char **p) --- p is a pointer tot pointer to char ===>  p gets the base address of argv = 1000

{

    char *t; --- t is pointer to char

    t = (p += sizeof(int))[-1]; ===> t = ( ( p + = sizeof(int) ) [ -1 ] ; ===> t = ( ( p = (p+ sizeof(int) ) ) [ -1 ] ) 

    printf("%s\n", t);

}

p -- is a ponter to the array argv,

p+0 points addr of 0th index of that array,

p+1 points 1 addr of 1st index of that array,....

p+4 points to addr of 4th index of that array,

p+5 points to addr of 5th index of that array.

p = (p+ sizeof(int) ) ====> assume size of int =4 Bytes ==> p=p+4 ===> addr of  4th index of that array, 

now p point at addr of 4th index of argv ===> p[0] = element of 4th index of argv ,  element of  p[1] = element of 5th index of argv , ... p[-1]= element of 3rd index of argv

p[-1] = element of 3rd index of that array ==> addr. 400

t= 3rd index of that array = 400 ===> addr. 400 converted as character pointer.

  printf("%s\n", t); ==> printing the string upto null character enters which is pointed by t ===> o/p:- gh

edited by

Related questions

2 votes
2 votes
1 answer
2
Sugumaran asked Feb 7, 2018
482 views
#include<stdio.h int main() { int x=191; char *p; p=(char*)&x; printf("%d",*p); }explain it
1 votes
1 votes
1 answer
3
hacker16 asked Jan 21, 2018
268 views
which function will going to be call first?x = f1() + f2()is it compiler dependent ?
1 votes
1 votes
1 answer
4
eyeamgj asked Nov 22, 2017
733 views
main(){int a;scanf("%i"&a);printf("%i %d",a,a);scanf("%d",&a);printf("%i %d",a,a);}suppose user entered 0101 as input .what is printed by above program?