edited by
904 views
2 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

1 Answer

1 1 vote

  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
Position:
Show:

Related questions

0 0 votes
1 1 answer
720
720 views
gatecse asked Dec 9, 2020
720 views
Match the following:$$\begin{array}{|l|l|l|l|} \hline (1) \text{Waterfall model} & (a) \text{Specifications can be developed} \\& \text{incrementally} \\\hline (2) \tex...
8 8 votes
1 answers 1 answer
7.4k
7.4k views
sh!va asked May 7, 2017
7,384 views
Estimation at software development effort for organic software in basic COCOMO is:E = 2.0 (KLOC) 1.05 PME = 3.4 (KLOC) 1.06 PME = 2.4 (KLOC) 1.05 PME = 2.4 (KLOC) 1.07...
0 0 votes
1 1 answer
737
737 views
go_editor asked Sep 18, 2018
737 views
What will be the output of the following C program? If you think it will give a runtime error, you need to mention it. In either case, your answer must include proper jus...
2 2 votes
1 1 answer
776
776 views
Sugumaran asked Feb 7, 2018
776 views
#include<stdio.h int main() { int x=191; char *p; p=(char*)&x; printf("%d",*p); }explain it