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 1 of 3.
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]);
Saleh Tarek said:
8 years ago
It is very easy:
1-char **p=argv. As argv is an array --> it is a pointer to its 1st element ("ab") --> argv=&("ab").
2- Now: t = (p+= sizeof(int))[-1];
* (p+= sizeof(int) -> p=p+2.
*As p=argv and argv is an array of pointers --> So, size of each element in argv array is 4-bytes--->As a result, +2=+8 bytes. (As sizeof(int=2-bytes)
So, p=&("ef").
Now:
t= &("ef")[-1] is equivalent to &("ef") - 4 (As we mentioned before sizeof("ef") =4-bytes)
So, t=&("cd").
1-char **p=argv. As argv is an array --> it is a pointer to its 1st element ("ab") --> argv=&("ab").
2- Now: t = (p+= sizeof(int))[-1];
* (p+= sizeof(int) -> p=p+2.
*As p=argv and argv is an array of pointers --> So, size of each element in argv array is 4-bytes--->As a result, +2=+8 bytes. (As sizeof(int=2-bytes)
So, p=&("ef").
Now:
t= &("ef")[-1] is equivalent to &("ef") - 4 (As we mentioned before sizeof("ef") =4-bytes)
So, t=&("cd").
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.
Sanjana said:
7 years ago
@All.
There must be;
t=(*p+=sizeof(int))[-1];
let's say argv=address of ab.
let's say argv=100.ie ab is stored at address 100.
now argv is sent to a function which takes something which points to char array ie argv
So, p points to argv.
let's say argv has address 200 so p=200.
Now we cannot directly increment p we need to increment *p.
but its written t=(p+=sizeof(int))[-1];
There must be;
t=(*p+=sizeof(int))[-1];
let's say argv=address of ab.
let's say argv=100.ie ab is stored at address 100.
now argv is sent to a function which takes something which points to char array ie argv
So, p points to argv.
let's say argv has address 200 so p=200.
Now we cannot directly increment p we need to increment *p.
but its written t=(p+=sizeof(int))[-1];
(3)
Ashish Bhardwaj said:
8 years ago
@Sahil, it is t= (p+=sizeof(int)) [-1]; and it works like this.
First, sizeof(int) is replaced by 2.
Now, p is of char type so it's incremented by 2bytes during assigning p=p+2. Till this time it's pointing "ef" after this it will point "cd" after executing [-1]. That's all.
First, sizeof(int) is replaced by 2.
Now, p is of char type so it's incremented by 2bytes during assigning p=p+2. Till this time it's pointing "ef" after this it will point "cd" after executing [-1]. That's all.
(4)
Rahul said:
1 decade ago
initally **p contains address of argv[0] element ie "ab".it is then incremented by 2 (size of int given as 2 bytes).now it point to "ef".It is then decremented by -1.so finally points to "cd".so it print value of t as "cd".
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
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.
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.
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?
(p+= sizeof(int))[-1];
If you look at the above statement, the parenthesis separates the increment part and (-1). Does this performs decrements operation?
Youssef said:
1 decade ago
Rahul said i don't think it decrement it
p[-1]=p-1
but not
p[-1] = (p=p-1) it dose not affect the result to the var, it just takes the value and decrement it but dose not save it to the variable
p[-1]=p-1
but not
p[-1] = (p=p-1) it dose not affect the result to the var, it just takes the value and decrement it but dose not save it to the variable
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers