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);
}
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.
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.
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.
CeParth said:
1 decade ago
OK with Turbo Logic.
But, gcc compiler prints "cd".
Why ?
But, gcc compiler prints "cd".
Why ?
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]);
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]);
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
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
DIPAYAN said:
1 decade ago
But if we remove [-1] it is giving no result. Why?
Mahesh said:
9 years ago
I can't understand the logic. Please explain me.
Dhrn said:
9 years ago
How this program work under linux gcc compiler?
Amit said:
9 years ago
In an online compiler, it shows gh then how it is cd?
Sahil said:
9 years ago
Please, explain t = (p+sizeof(int))[-1].
(1)
Mohit said:
9 years ago
sizeof(int) = 4 not 2.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers