Search Placementyogi.com

Pointers in C Objective Questions and Answers

:: Topics ::

 1 Which are the correct usage of pointer definition and initialization? (more than one correct) A. int a=10;   int *ptr=a; B. int a=10; *ptr; ptr=a; C. int a=10; *ptr; *ptr = &a; D. int a=10; int *ptr=&a; E. int a=10; *ptr; ptr=&a;
See Answer & Explanation Lets Discuss
Explanation

Points to remember

1. A pointer is a variable that stores the address of other variable.

EX. int a, *ptr; // 'ptr' is a pointer variable

a=20;

ptr=&a;    //Storing address of int variable 'a' in ptr pointer

Now a and ptr are pointing to same memory location.

2. *p means value at address at which p is pointing.

&a means address of variable a.

Option A :int *ptr=a stores value of a in ptr which is not correct, it shoud be => int *ptr=&a;

Option B :  ptr=a; storing value of a, which is not an address. it should be => ptr = &a;

Option C :  *ptr = &a; here we are trying to store address of a at address where ptr is pointing, but ptr is pointing no where. it should be => ptr = &a;

 2 What is the output of below program?( Linux 32 bit )  ```#include int main() { int *ptr1; float *ptr2; char *ptr3; printf("ptr1=%d,",sizeof(ptr1)); printf("ptr2=%d,",sizeof(ptr2)); printf("ptr3=%d",sizeof(ptr3)); }``` A. 4, 4, 1 B. 4, 8, 1 C. 8, 8, 1 D. 4, 4, 4
See Answer & Explanation Lets Discuss
Explanation

Surprised?? yes pointer variable size is fixed on machine regardless of what is being pointed to.

The size of a pointer is dependent upon the architecture of the computer — a 32-bit computer uses 32-bit memory addresses — consequently, a pointer on a 32-bit machine is 32 bits (4 bytes). On a 64-bit machine, a pointer would be 64 bits (8 bytes).

 3 Point out error in below program if any? ```#include int main() { int a=20; void *ptr; //Line 5 ptr=&a; //Line 6 printf("%d",*ptr); //Line 7 }``` A. At Line 5, pointer type can not be void B. At Line 6, int type variable address can not be stored in void pointer C. At Line 7, dereferencing ‘void *’ pointer D. No error,it prints 20
See Answer & Explanation Lets Discuss
Explanation

Points to remember

Void *ptr is a void pointer declaration, also known as the generic pointer, is a special type of pointer that can be pointed at variable of any data type!

```Ex. void *ptr;
int a; float b; char c;
ptr = &a;  //valid
ptr = &b;  //valid
ptr = &c;  //valid

printf("%d",*ptr); //Not valid```

void pointer can not be dereferenced because it does not know the type of variable it is pointing. Rather, it must first be explicitly cast to proper type before it is dereferenced.

```EX. printf("%d",*(int*)ptr); //valid
printf("%d",*(float*)ptr); //valid
printf("%d",*(char*)ptr); //valid```

 4 What does *p++ do? A. increments p B. increments value pointed by p C. error D. none
See Answer & Explanation Lets Discuss
Explanation

Here the precedence of ++ is higher than * so pointer p will be incremented.

 5 What will be the output of the following c program? ```#include int main(void) { char a = 'A'; char * const ptr; //Line 5 ptr = &a; //Line 6 printf("%d",*ptr); }``` A. A B. 65 C. error: assignment of read-only variable ‘ptr’ D. garbage value
See Answer & Explanation Lets Discuss
Explanation

Look at the line 5:

char * const p

Here ptr is a constant pointer to a variable of type char, and we can not modify the ptr value at line 6.

To understand such complex declarations you need to learn reading Complex Declaration using Right-Left Rule. Here is a small walk through to read declarations like this:

Starting from the variable name first consider what is written on the RIGHT hand side of variable name , then Look at what is present on the LEFT hand side, then again see what is present on the right hand side, go on like this till the whole expression is read.

If a semicolon is encountered on the Right side, only read the Left side.

Consider the expression: int **ptr[];

Step 1.  Here the variable name is "ptr"

Step 2. See what is present on the right hand side of the variable name , i.e. [] (square brackets)

EX. It means ptr is an array (ptr[])

Step 3. Now look at the left side of ptr, a * is present which means:

ptr is an array of pointers (*ptr[])

Step 4. Again look at what is present on the right hand side of the expression already read i.e.

what is present on the right side of *ptr[], Nothing is present, So we will look at the LEFT side of the expression, a "*" is present which means

ptr is an array of pointer to pointer (**ptr[]).

Step 5.  Since there was a semicolon at the Right side, we will only look at Left side . The keyword int is present. Hence,

ptr is an array of pointers to pointer of integer type variables (int **ptr[])

Hide Page Information

C Pointers Objective Questions and Answers for Written Round Interview

Here you can find C Pointers Objective Questions with answers and explanation. We have a vast collection of Objective Questions on Pointers in C that will help you a lot!

Why learn C Pointers?

Pointers are an integral part of C and many Objective Questions in the written round are asked from this topic.In this section you can learn and practice C Pointers (Objective Questions with Answers) to improve your skills in order to face the interview and clear campus and off-campus selection of TCS, Infosys, Wipro etc.

Where can I get C Pointers Objective Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved C Pointers Objective Questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.Though we do not have pdf file for C Pointers, We have quiz Objective Questions with answers to help you test yourself..

\n

Where can I get C Pointers Interview Objective Questions and Answers (objective type, multiple choice)?

Here you can find objective type C Pointers Objective Questions and answers for interview and entrance examination.Multiple Answers and true or false type Objective Questions are also provided.

How to solve C Pointers problems?

You can easily solve all kind of Objective Questions based on C Pointers by practicing the exercises (including shortcut Pointers to solve problems) given below.