Professional Documents
Culture Documents
void main()
{
int arr[]={8,3,5,1,6};
int *ptr;
ptr=&arr[2];
printf("%d",ptr[-1]);
}
Compilation Error
4
3
2
Explanation:
ptr=&arr[2];// Will store the address of value 5 in ptr
printf("%d",ptr[-1]);
ptr[-1] --> *(address stored in ptr -1) which points to
nothing but the arr[1] which is 3. Hence, this option is correct
for(i=0;i<3;i++)
{
int i=100;
i--;
printf("%d..",i);
}
}
99..99..99..
0..1..2..
99..98..97..
100..100..100..
Explanation:
This is correct answer. While printing i the innermost one i.e., int i=100; is in scope. Hence, always
99 is printed.
6
9
13
Explanation:
Correct Answer is 9.
2.What is the output of the below C program ?
void display_arr(int **ptr)
{
printf(" 0 -> %d 1 -> %d 2 -> %d 3 -> %d\n",ptr[0][0],ptr[0][1],ptr[0][2],ptr[0][3]);
}
void main()
{
int arr[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
int **int_arr;
int_arr = (int *)arr;
display_arr(int_arr);
int_arr++;
display_arr(int_arr);
}
Segmentation Fault
0 -> 0 1 -> 1 2 -> 2 3 -> 3 0 -> 8 1 -> 9 2 -> 10 3 -> 11
0 -> 0 1 -> 1 2 -> 2 3 -> 3 0 -> 4 1 -> 5 2 -> 6 3 -> 7
0 -> 4 1 -> 5 2 -> 6 3 -> 7 0 -> 8 1 -> 9 2 -> 10 3 -> 11
Explanation:
The pointer "int **int_arr;" declared for the two dimensional array here is not correct. Hence when
we try to pass this as a argument to the function "void display_arr(int **ptr)" it results in a
Segmentation Fault. Correct way of pointer deceleration is shown below,
void display_arr(int (*ptr)[4])
{
printf(" 0 -> %d 1 -> %d 2 -> %d 3 -> %d\n",ptr[0][0],ptr[0][1],ptr[0][2],ptr[0][3]);
}
void main()
{
int arr[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
int (*int_arr)[4]; //Is the proper declaration int_arr here is a pointer to an array of 4 integers
int_arr = arr;
display_arr(int_arr);
int_arr++;
display_arr(int_arr);
}
#include
int main()
{
int a=0;
if (a = 2 )
{
printf("\nI am inside if part\n ",a);
}
else
{
printf("\nI am inside else part\n ",a);
}
return 0;
}
I am inside if part
Run Time Error
Compilation Error
I am inside else part
Explanation:
This is correct answer. I am inside if part.
Observe carefully in the above if check we have if (a = 2 ) and not if (a == 2 ).
So a will be assigned value 2. So the condition (a = 2)evaluates to 2. Anything > 0 is TRUE. Hence
we will enter in the first if check.