edited by
678 views
3 votes
3 votes

What is the output of the following program?

#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);
}
  1. ab
  2. cd
  3. ef
  4. gh
edited by

2 Answers

Best answer
4 votes
4 votes

sizeof(int) is compiler dependent ..  so from this line 

t = (p += sizeof(int))[-1];  
what we get is

if sizeof(int) = 2
 [ in 16 bit systems it is 2 bytes, which is now obsolate we don't use 16 bit computer now a days] 

then  
t = (p += sizeof(int))[-1] 
t = (p = p + 2)[-1] 
t = *(p + 2 -1) = *(p+1) = address of second element i.e. 'cd' 

--------

if sizeof(int) = 4 [ in 32 bit systems size of int = 4 B,  we use 32 Bit systems now a days ]
then  

t = (p += sizeof(int))[-1] 
t = (p = p + 4)[-1] 
t = *(p + 4 -1) = *(p+3) = address of forth element i.e. 'gh'. 

..................

#include <stdio.h>  
void f(char**);   
int main()  
{  
char *argv[ ] = { "ab", "cd", "ef", "gh", "ij", "kl" };     // it creates character array.

 //    In char array address point to only starting of character array. 


f(argv);      // function call with base address of char array.   
return 0;  


void f(char **p)       // function call comes here. here p is pointer to pointer array

{  
char *t;  //create pointer variable  
t = (p += sizeof(int))[-1];         //  t = (p = p+4 ))[-1];    

                                              // p point to "gh" starting of this char array.

                                            //     now P[-1] ; t= *(p-1); now t point to  "gh" array . 

                                       

  
printf("%s\n", t);                // %s this tell to print string until % not encounter .

since every string stored end with %.  
}

so it prints 'gh' 

selected by
3 votes
3 votes

actually sizeof(int) is compiler dependent ..  so from this line 

t = (p += sizeof(int))[-1];  
what we get is
if sizeof(int) = 2 
[ in 16 bit systems it is 2 bytes, which is now obsolete we don't use 16 bit computer now a days] 

then  
t = (p += sizeof(int))[-1] 
t = (p = p + 2)[-1] 
t = *(p + 2 -1) = *(p+1) = address of second element i.e. 'cd' 

--------------
if sizeof(int) = 4 [ in 32 bit systems which is used now a days ]

then  

t = (p += sizeof(int))[-1] 
t = (p = p + 4)[-1] 
t = *(p + 4 -1) = *(p+3) = address of forth element i.e. 'gh'. 

..................

#include <stdio.h> 
void f(char**);  
int main() 

char *argv[ ] = { "ab", "cd", "ef", "gh", "ij", "kl" };        // it creates character array.

     //   In char array address point to only starting of character array. 


f(argv);               // function call with base address of char array.  
return 0; 


void f(char **p)             // function call comes here. here p is pointer to pointer array


char *t;                             //create pointer variable 
t = (p += sizeof(int))[-1];            //t = (p = p+4 ))[-1];

                                          // p point to "gh" starting of this char array.

                                       //       now P[-1] ; t= *(p-1); now t point to  "gh" array . 

 
printf("%s\n", t);               // %s this tell to print string until % not encounter .

since every string stored end with %. 
}

so it prints 'gh' 

The size of an int is really compiler dependent. Back in the day, when processors were 16 bit, an int was 2 bytes. Nowadays, it's most often 4 bytes on a 32 bits system or 8 bytes on 64 bits system

http://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes

edited by
Answer:

Related questions

3 votes
3 votes
1 answer
1
Bikram asked May 14, 2017
392 views
Assume that $a[4]$ is a one-dimensional array of $4$ elements, $p$ is a pointer variable and $p = a$ is performed. Now, which among these expressions is illegal?$p == a[0...
0 votes
0 votes
1 answer
2
Bikram asked May 14, 2017
351 views
Spot the error(s) in this code snippet :int n=2; // Line 1 switch(n) { case 1.5: printf( "gate"); break; case 2: printf( "overflow"); break; case 'A': printf("gateoverflo...
0 votes
0 votes
1 answer
3
Bikram asked May 14, 2017
199 views
#include<stdio.h int K = 10; int main() { foo(); foo(); return 0; } int foo() { static int k= 1; printf("%d ",k); k++; return 0; }What is the output of the above code sni...
0 votes
0 votes
1 answer
4
Bikram asked May 14, 2017
202 views
What is the output of the following program? int fun (int z) { if( z==0 || z==1 ) return 1; else return fun(z-1) ; } int main() { int y; y=fun(8); printf(“%d”...