C Programming :: Pointers

Home > Technical Aptitude > C Programming > Pointers > General Questions

NA
SHSTTON
7
Solv. Corr.
9
Solv. In. Corr.
16
Attempted
0 M:0 S
Avg. Time

1 / 59

What is the output of the following C Program?
#include<stdio.h>
void main()
{
char *p;
int *q;
long *r;
p=q=r=0;
p++;
q++;
r++;
printf("%p...%p...%p",p,q,r);
}

AGarbage Value

B0001...0002...0004

CCompilation Error

DNone of these

Explanation:

++ operator when applied to pointers increments address according to their corresponding data-types.

Workspace

NA
SHSTTON
4
Solv. Corr.
12
Solv. In. Corr.
16
Attempted
0 M:0 S
Avg. Time

2 / 59

What is the output of the following C Program?
#include<stdio.h>
void main()
{
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}

A1 2

B1 1

C2 2

D2 1

ENone of these

Explanation:

The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

Workspace

NA
SHSTTON
1
Solv. Corr.
12
Solv. In. Corr.
13
Attempted
0 M:0 S
Avg. Time

3 / 59

What is the output of the following C Program?
#include<stdio.h>
void main()
{
char far *farther,*farthest;

printf("%d   %d",sizeof(farther),sizeof(farthest));
}

A4 4

B2 4

C2 2

D4 2

ENone of these

Explanation:

the second pointer is of char type and not a far pointer

Workspace

NA
SHSTTON
11
Solv. Corr.
3
Solv. In. Corr.
14
Attempted
0 M:0 S
Avg. Time

4 / 59

What is the output of the following C Program?
#include<stdio.h>
void main()
{
int *j;
{
int i=10;
j=&i;
}
printf("%d",*j);
}

AGarbage Value

B10

DNone of these

Explanation:

The variable i is a block level variable and the visibility is inside that block only. But the lifetime of i is lifetime of the function so it lives upto the exit of main function. Since the i is still allocated space, *j prints the value stored in i since j points i.

Workspace

NA
SHSTTON
4
Solv. Corr.
10
Solv. In. Corr.
14
Attempted
0 M:0 S
Avg. Time

5 / 59

What is the output of the following C Program?
#include<stdio.h>
int main()
{
char *p;
p="Hello";
printf("%c\n",*&*p);
return 0;
}

AHello

Bello

CH

DHe

ENone of these

Explanation:

* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again
& references it to an address and * dereferences it to the value H.

Workspace

NA
SHSTTON
6
Solv. Corr.
6
Solv. In. Corr.
12
Attempted
0 M:0 S
Avg. Time

6 / 59

What is the output of the following C Program?
#include<stdio.h>
int main()
{
char *cptr,c; void *vptr,v; c=10; v=0;
cptr=&c; vptr=&v;
printf("%c%v",c,v);
return 0;
}

ACompiler error : size of v is Unknown.

B%v

C10 %v

D10

ENone of these

Explanation:

You can create a variable of type void * but not of type void, since void is an empty type. In the second line you are creating variable vptr of type void * and v of type void hence an error.

Workspace

NA
SHSTTON
5
Solv. Corr.
6
Solv. In. Corr.
11
Attempted
0 M:0 S
Avg. Time

7 / 59

What is the output of the following C Program?
#include<stdio.h>
int one_d[]={1,2,3};
int main()
{
int *ptr;
ptr=one_d;
ptr+=3;
printf("%d",*ptr);
return 0;
}

A1

B2

C3

Dgarbage value

ENone of these

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
2
Solv. Corr.
9
Solv. In. Corr.
11
Attempted
0 M:0 S
Avg. Time

8 / 59

What is the output of the following C Program?
#include<stdio.h>
int main( ){
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf("%u %u %u %d \n",a,*a,**a,***a);
printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1);
}

A1000, 1000, 1000, 21014, 1004, 1002, 3

B1002, 1002, 1002, 41014, 1004, 1008, 3

C1000, 1000, 1000, 41014, 1004, 1002, 3

D1000, 1000, 1000, 21014, 1004, 1002, 3

ENone of these

Explanation:

The given array is a 3-D one. It can also be viewed as a 1-D array.
2 4 7 8 3 4 2 2 2 3 3 4
1000 1002 1004 1006 1008 1010 1012 1014 1016 1018 1020 1022
thus, for the first printf statement a, *a, **a give address of first element .
since the indirection ***a gives the value. Hence, the first line of the output.
The second printf a+1 increases in the third dimension thus points to value at 114, *a+1 increments in second dimension thus points to 104, **a +1 increments the first dimension thus points to 102 and ***a+1 first gets the value at first location and then increments it by 1.

Workspace

NA
SHSTTON
4
Solv. Corr.
4
Solv. In. Corr.
8
Attempted
0 M:0 S
Avg. Time

9 / 59

What is the output of the following C Program?
#include<stdio.h>
int main(){
void *vp;
char ch = 'g', *cp = "goofy";
int j '= 20;
vp = &ch;
printf("%c", *(char *)vp);
vp = &j;
printf("%d",*(int *)vp);
vp = cp;
printf("%s",(char *)vp + 3);
}

Agoofy20

Bg20fy

Cgg20fy

DCompilation error

Explanation:

Since a void pointer is used it can be type casted to any other type pointer. vp = &ch stores address of char ch and the next statement prints the value stored in vp after type casting it to the proper data type pointer. the output is "g".
Similarly the output from second printf is "20". The third printf statement type casts it to print the string from the 4th value hence the output is "fy".

Workspace

NA
SHSTTON
4
Solv. Corr.
7
Solv. In. Corr.
11
Attempted
0 M:0 S
Avg. Time

10 / 59

What is the output of the following C Program?
#include<stdio.h>
int main()
{
char *p;
p="%d\n";
p++; p++;
printf(p-2,300);
return 0;
}

ACompilation Error

B300

Cno output No Error

DGarbage Value

ENone of these

Explanation:

The pointer points to % since it is incremented twice and again decremented by 2, it points to '%d\n' and 300 is printed.

Workspace