Machine-translated by Google. Questions are authored in English.
Top

Pointers Questions and Answers

101.

What is the output of this C code?

void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int *r = &p;
printf("%d", (**r));
}

 

Answer: B

Correct answer: Compile time error

p is an int *, so &p has type int **, and initialising an int * from it is a constraint violation - the levels of indirection do not match. The declaration should read int **r = &p;, which is exactly what the following question does. Some compilers downgrade this to a warning, but it is an error in standard C.

Please log in or register to report this question.

102.

What is the output of this C code?

void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int **r = &p;
printf("%p %p", *r, a);
}

 

Answer: C

Correct answer: Same address is printed.

r holds the address of p, so *r is p itself, and p was set from a, which decays to the address of its first element. So *r and a print the same address. Note that the levels are now consistent, unlike the previous question, which is why this one compiles.

Please log in or register to report this question.

103.

How many number of pointer (*) does C have against a pointer variable declaration?

Answer: D

Correct answer: No limits

The standard sets no limit on the number of levels of indirection, and a conforming implementation must support at least twelve pointer, array and function declarators modifying a type. In practice more than two or three levels is a sign that the design would be better served by a structure or a typedef, since the declarations become almost unreadable.

Please log in or register to report this question.

104.

What is the output of this C code?

int main()
{
int a = 1, b = 2, c = 3;
int *ptr1 = &a, *ptr2 = &b, *ptr3 = &c;
int **sptr = &ptr1; //-Ref
*sptr = ptr2;
}

 

Answer: B

Correct answer: ptr1 points to b

sptr holds the address of ptr1, so *sptr is ptr1, and assigning ptr2 to it copies the address of b into ptr1. Afterwards ptr1 and ptr2 both point at b. Note that sptr itself is unchanged and still points at ptr1.

Please log in or register to report this question.

Answer: C

Correct answer: The value stored at the address held by p

The asterisk is the dereference operator, which follows the pointer to the object it refers to. The address of p itself is obtained with &p, and p alone gives the address it holds.

Please log in or register to report this question.

106.

What substitution should be made to //-Ref such that ptr1 points to variable C?

int main()
{
int a = 1, b = 2, c = 3;
int *ptr1 = &a;
int **sptr = &ptr1;
//-Ref
}

 

Answer: A

Correct answer: *sptr = &c;

*sptr is another name for ptr1, so assigning the address of c to it makes ptr1 point at c. The alternative **sptr = &c; would assign a pointer to the int that ptr1 currently points at, and *ptr1 = &c; does the same thing - both are type errors and both write to a rather than redirecting the pointer.

Please log in or register to report this question.

107.

Which of the following declaration throw run-time error?

Answer: D

Correct answer: None of the mentioned

None of the three reaches run time. Each is a type error caught at compilation: &c in the first has type int ***, the second is the same after the & and * cancel, and the third dereferences c twice in its own initialiser. A run-time error would require a program that compiles first.

Please log in or register to report this question.

Answer: B

Correct answer: We don't have address of an address operator

There is no && address operator in C. The token exists only as logical AND, so &&a is parsed as that operator with a missing left operand and the line will not compile. To obtain a pointer to a pointer you must have a pointer object to take the address of, as the earlier questions do with &p.

Please log in or register to report this question.

109.

Which of the following changes can correct the program so that it prints “Geeks Quiz”?

#include <stdio.h> 
  
void myStrcat(char *a, char *b) 
{ 
    int m = strlen(a); 
    int n = strlen(b); 
    int i; 
    for (i = 0; i <= n; i++) 
       a[m+i]  = b[i]; 
} 
  
int main() 
{ 
    char *str1 = "Geeks "; 
    char *str2 = "Quiz"; 
    myStrcat(str1, str2); 
    printf("%s ", str1); 
    return 0; 
} 

 

Answer: A

Correct answer: char *str1 = “Geeks “; can be changed to char str1[100] = “Geeks “;

The fault is that str1 points at a string literal, which must not be modified, and there is no room after it in any case. Making it an array of 100 characters gives writable storage with space to spare. No terminator line is needed, because the copy loop runs to i <= n and so copies b’s own null byte along with its characters.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Pointers Questions and Answers || Pointers MCQ Questions and Answers || Pointers GK Questions and Answers || Pointers GK MCQ Questions || Pointers Multiple Choice Questions and Answers || Pointers GK || GK on Pointers || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming