C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 17)
17.
If int is 2 bytes wide.What will be the output of the program?
#include <stdio.h>
void fun(char**);

int main()
{
    char *argv[] = {"ab", "cd", "ef", "gh"};
    fun(argv);
    return 0;
}
void fun(char **p)
{
    char *t;
    t = (p+= sizeof(int))[-1];
    printf("%s\n", t);
}
ab
cd
ef
gh
Answer: Option
Explanation:

Since C is a machine dependent language sizeof(int) may return different values.

The output for the above program will be cd in Windows (Turbo C) and gh in Linux (GCC).

To understand it better, compile and execute the above program in Windows (with Turbo C compiler) and in Linux (GCC compiler).

Discussion:
26 comments Page 2 of 3.

Mahesh said:   9 years ago
I can't understand the logic. Please explain me.

DIPAYAN said:   1 decade ago
But if we remove [-1] it is giving no result. Why?

Oshin said:   1 decade ago
t = (p+=sizeof(int))[-1];
t = (p=p+2)[-1];

t = (p+2)[-1];//(p+2) refers to the base address of p +'2'

//We know that num[i]=*(num+1): refer to let us c ch 8 topic the real thing.(read completely)

t = *(p+2-1);
t = *(p+1);

//Output: cd acc. to turbo c

Shashi said:   1 decade ago
This program make use of two properties of pointers to array.

1. For pointer to an array, subscript value will also work with pointer. because behind the scenes in C/C++, a[2] is essentially *(a+2) making use of pointer
by the by, 2[a] will also work the same way(No Error here).

2. a[-1] will be *(a-1) but it will only work if a is pointer to the array and pointing somewhere in middle. if a is an array, 'a' will b base address of pointer and a[-1] will go out of bounds of Array printing a garbage value in the process.


Eg:

// initialized the array.
int a[] = {0,1,2,3,4};

// given pointer the address of a[2].
int *p = a+2;

// p points to a[2].
// it'll print 1.
printf("%d", p[-1]);

CeParth said:   1 decade ago
OK with Turbo Logic.

But, gcc compiler prints "cd".
Why ?

Aakash said:   1 decade ago
Guys.

char *argv is a array of pointers to string.
p contains the base address of argv. p points to argv. and argv[] points to base address of the respective strings.

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

p=p+2 means p will point 2 locations ahead of its type. means argv[2]. and then p[-1], we can write it as *(p-1) so now it will point to argv[1]. due to * the base address of "cd" which is contained in argv[1] get returned in t and cd get printed.

Rajesh.T.K. said:   1 decade ago
This is the statement given in the program:

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

If you look at the above statement, the parenthesis separates the increment part and (-1). Does this performs decrements operation?

Suhas said:   1 decade ago
Explanation:
Firstly, t = (p+= sizeof(int))[-1]; is treated as
t=(p=p+(size of int) (-1));
So p="ab" and ab+2-1 becomes cd so it prints "cd" finally.

Thank you..

Kishan said:   1 decade ago
Here char *argv[]={"ab", "cd", "ef", "gh"};.

Than call fun (argv) ;.

In char **p there is "ab".

Then sizeof (int) means 2 and then 2-1=1 store in p.

So in t=1 so ab=0 cd=1 ef=2 gh=3 in array start with 0.

So print cd.

Mouneesha said:   1 decade ago
In general size of(int)=4 so in linux it gives p+=4-1=3 i.e index 3 gh
but they have given int is 2 bytes so it gives in windows index 1 i.e cd.


Post your comments here:

Your comments will be displayed after verification.