• retagged by
1,658 views
3 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

2 Answers

Best answer
4 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 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:
Position:
Show:

Related questions

3 3 votes
2 answers 2 answers
1.3k
1.3k views
Bikram asked May 14, 2017
1,298 views
What is the output of this program?#include <stdio.h int main() { char *ptr; char string[] = "How are you?"; ptr = string; ptr += 4; printf("%s",ptr); return 0; }How are ...
4 4 votes
2 answers 2 answers
2.0k
2.0k views
Bikram asked May 14, 2017
1,975 views
State the output in below code snippet :#include <stdio.h int main(void) { char x[5] = { 1, 2, 3, 4, 5 }; char *ptr = (char*)(&x + 1); printf("%d %d\n", *(x + 1), *(ptr -...
4 4 votes
1 answers 1 answer
1.0k
1.0k views
Bikram asked May 14, 2017
1,032 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...
4 4 votes
1 answers 1 answer
1.5k
1.5k views
Bikram asked May 14, 2017
1,520 views
Point out the error : #include <stdio.h void f(int*); int main() { int i=35,*z; z=f(&i); printf("%d",z); return 0; } void f(int*m) { return(m+2); }error in function calle...