0% found this document useful (0 votes)
4K views

C Pointers MCQs

C Pointers MCQs

Uploaded by

patilthetrainer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views

C Pointers MCQs

C Pointers MCQs

Uploaded by

patilthetrainer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. What is the output of this C code?

int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}

 A. nullp nullq

 B. Depends on the compiler

 C. x nullq where x can be p or nullp depending on the value of NULL

 D. p q

2. What is the output of this C code?


int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}

Correct one is : printf("%d\n", *(int*)p);

 A. Compile time error

 B. Segmentation fault/runtime crash

 C. 10

 D. Undefined behaviour

Dereferencing a void Pointer


We can't just dereference a void pointer using indirection (*) operator

void *vp;
int a = 100;

vp = &a;
printf("%d", *vp); //
wrong
It simply doesn't work that way!. Before you dereference a void pointer it must be typecasted to appropriate pointer type. Let me
show you what I mean.

For example: In the above snippet void pointer vp is pointing to the address of integer variable a. So in this case vp is acting as a
pointer to int or (int *). Hence the proper typecast in this case is (int*).

(int *)vptr
Now the type of vptr temporarily changes from void pointer to pointer to int or (int*) , and we already know how to dereference
a pointer to int, just precede it with indirection operator (*)

*(int *)vptr

Note: typecasting changes type of vp temporarily until the evaluation of the expression, everywhere else in the program vp is still a
void pointer.

The following program demonstrates how to dereference a void pointer.

5
#include<stdio.h>
#define SIZE 10
6
int main()
7
{
int i = 10;
8
float f = 2.34;
char ch = 'k';
9
1
void *vptr;
0
1
vptr = &i;
1
printf("Value of i = %d\n", *(int
1
*)vptr);
2
1
vptr = &f;
3
printf("Value of f = %.2f\n", *(float
1
*)vptr);
4
1
vptr = &ch;
5
printf("Value of ch = %c\n", *(char
1
*)vptr);
6
1
// signal to operating system program ran
7
fine
1
return 0;
8
}
1
9
2
0
2
1
2
2
2
3

2. What is the output of this C code?

int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}

 A. Compile time error

 B. Undefined behaviour

 C. 10

 D. 0.000000
2. what is the output of this code

// function to pointer

#include<stdio.h>

int *f();
int main()
{
int *p = f();
printf("\n %d", *p);
}

int *f()
{
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}

What is the output of this C code?

int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}

Which is an indirection operator among the following?

 A. &

 B. *

 C. ->

 D. .

The unary indirection operator (*) accesses a value indirectly, through a pointer. The operand must be a pointer
type. The result of the operation is the value addressed by the operand.

What is the output of this C code?

int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}

What is the output of this C code?

int x = 0;
void main()
{
Int x=10;
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}

 A. Same address

 B. Different address

 C. Compile time error

 D. Varies

What is the output of this C code?

int x = 0;
void main()
{
int *const ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}

 A. 0 1

 B. Compile time error

 C. 0xbfd605e8 0xbfd605ec

 D. 0xbfd605e8 0xbfd605e8

What is the output of this C code?

void main()
{
int x = 0;
int *ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}

What is the output of this C code?

void main()
{
int x = 0;
int *ptr = &5;
printf("%p\n", ptr);
}

 A. 5

 B. Address of 5

 C. Nothing

 D. Compile time error

What is the output of this C code?

void main()
{
int x = 0;
int *ptr = &x;
printf("%d\n", *ptr);
}

 A. Address of x

 B. Junk value

 C. 0

 D. Run time error

What is the output of this C code?

void func(int*);
int main()
{
int i = 10;

func((&i)++);
}
void func(int *p)
{
printf("%d\n", *p);
}

 A. 10

 B. Some garbage value

 C. Compile time error

 D. Segmentation fault/code crash

What is the output of this C code?

void func(int*);
int main()
{
int i = 10, *p = &i;

func(p++);
}
void func(int *p)
{
printf("%d\n", *p);
}

 A. 10

 B. Some garbage value

 C. Compile time error

 D. Segmentation fault

What is the output of this C code?

int main()
{
int i = 97, *p = &i;
func(&i);
printf("%d ", *p);
}
void func(int *p)
{
int j = 2;
p = &j;
printf("%d ", *p);
}

 A. 2 97

 B. 2 2

 C. Compile time error

 D. Segmentation fault/code crash

What is the output of this C code?

int main()
{
int i = 97, *p = &i;
func(&p);
printf("%d ", *p);
return 0;
}
void func(int **p)
{
int j = 2;
*p = &j;
printf("%d ", **p);
}

 A. 2 2

 B. 2 97

 C. Undefined behaviour

 D. Segmentation fault/code crash

What is the output of this C code?

int main()
{
int i = 11;
int *p = &i;
foo(&p);
printf("%d ", *p);
}
void foo(int *const *p)
{
int j = 10;
*p = &j;
printf("%d ", **p);
}

 A. Compile time error

 B. 10 10

 C. Undefined behaviour

 D. 10 11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy