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

Pointers Questions and Answers

1.

What is the output of the following 'C' language statements?

#include\(\lt\)stdio.h\(\gt\)
int main(){
int a=10,b=20,*p1=&a,*p2=&b;
printf("Before : **p1=%d **p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter : **p1=%d **p2=%d",*p1,*p2);
return 0;
}

Exam Year(s)

DSSSB TGT - 2023,

Answer: A

The output is Before : **p1=10 **p2=20 and then After : **p1=20 **p2=10. The pointers p1 and p2 point to a and b, so the three statements swap the values without a temporary variable. \[ *p1 = 10 + 20 = 30 \] \[ *p2 = 30 - 20 = 10 \] \[ *p1 = 30 - 10 = 20 \] The strings '**p1=' and '**p2=' are printed as plain text. The second option reverses the values, the third drops the asterisks that the format string prints, and the fourth gives the wrong second value.

Please log in or register to report this question.

2.

What is the output of the following C program?

#include \(\lt\)stdio.h\(\gt\)
int main() {
  double a[2]={20.0, 25.0}, *p, *q;
  p = a;
  q = p + 1;
  printf("%d,%d", (int)(q - p), (int)(*q - *p));
  return 0;}

Exam Year(s)

GATE CS - 2024,

Answer: B

The output is 1,5.

p points to a[0] and q to a[1]. Subtracting two pointers into the same array gives the number of elements between them, not the number of bytes, so q - p is 1 even though a double occupies 8 bytes.

*q - *p subtracts the values stored there, \(25.0 - 20.0 = 5.0\), and the cast to int gives 5. Both arguments are ints, so the %d conversions print 1,5.

8 (in 8,5) is the byte distance, which pointer subtraction does not return. 4,8 and 1,8 mix up the element and byte counts, and the value difference is 5, never 8.

Please log in or register to report this question.

3.

Consider the following C program:

#include\(\lt\)stdio.h\(\gt\)

void fun1(char *s1, char *s2){
  char *tmp;
  tmp = s1;
  s1 = s2;
  s2 = tmp;
}
void fun2(char **s1, char **s2){
  char *tmp;
  tmp = *s1;
  *s1 = *s2;
  *s2 = tmp;
}
int main(){
  char *str1 = "Hi", *str2 = "Bye";
  fun1(str1, str2); printf("%s %s ", str1, str2);
  fun2(&str1, &str2); printf("%s %s", str1, str2);
  return 0;
}

The output of the program above is

Exam Year(s)

GATE CS - 2018,

Answer: A

The program prints Hi Bye Bye Hi.

fun1 receives copies of the two pointers. It swaps its own copies, but str1 and str2 in main are unchanged, so the first printf prints 'Hi Bye '.

fun2 receives the addresses of str1 and str2. Writing through *s1 and *s2 swaps the pointers in main itself, so str1 now points to "Bye" and str2 to "Hi", and the second printf prints 'Bye Hi'.

'Hi Bye Hi Bye' assumes neither function swaps anything. The options starting 'Bye Hi' assume fun1's swap reaches main, which C's pass-by-value rules out.

Please log in or register to report this question.

4.

What is printed by the following ANSI C program?

#include\(\lt\)stdio.h\(\gt\)
int main(int argc, char *argv[])
{
  int x = 1, z[2] = {10, 11};
  int *p = NULL;
  p = &x;
  *p = 10;
  p = &z[1];
  *(&z[0] + 1) += 3;
  printf("%d, %d, %d\n", x, z[0], z[1]);
  return 0;
}

Exam Year(s)

GATE CS - 2022,

Answer: D

The program prints 10, 10, 14.

  1. p = &x; *p = 10; writes 10 into x through the pointer.
  2. p = &z[1]; only moves p; nothing is written.
  3. &z[0] + 1 is the address of z[1], so *(&z[0] + 1) += 3 adds 3 to z[1], making it 14. z[0] stays 10.

So x = 10, z[0] = 10, z[1] = 14. '1, 10, 11' and '1, 10, 14' forget that x was changed through p. '10, 14, 11' applies the += 3 to z[0] instead of to the element one past it.

Please log in or register to report this question.

5.

Consider the case that a record is read by another, which is moved or deleted and it contains a pointer, then that pointer is called as :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

Such a pointer is called a dangling pointer.

If a record holding a pointer to another record keeps that pointer after the target has been moved or deleted, the pointer now refers to a location that no longer holds the intended record. That is a dangling pointer, and following it gives wrong data or an error. Database systems avoid it with tombstones or indirection through record identifiers.

A dynamic pointer is just one allocated at run time, a pointer to pointer holds the address of another pointer, and 'unlinked pointer' is not a standard term.

Please log in or register to report this question.

6.

What is the output of this C code?

void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **m);
}

 

Answer: A

Correct answer: 5 5 5

Each dereference removes one level. k is 5; p holds the address of k, so *p is 5; and m holds the address of p, so *m is p and **m is 5. All three names reach the same object by paths of different length, which is the whole point of the double pointer.

Please log in or register to report this question.

7.

What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

Answer: B

Correct answer: *(*(*(*(a+i)+j)+k)+l)

Each subscript is defined as a dereference of a sum: x[y] means *(x+y). Applying that rule four times, from the outside in, gives one dereference for every subscript.

\[ a[i][j][k][l] \equiv *(*(*(*(a+i)+j)+k)+l) \]

The options without the asterisks compute addresses but never read through them. Note that the pointer arithmetic is scaled by the size of the sub-array at each level, which is why the array's dimensions must be known to the compiler.

Please log in or register to report this question.

8.

What will be the output of the program If the integer is 4bytes long?

#include <stdio.h>

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

 

Answer: A

Correct answer: 8, 8, 8

Each level of indirection is undone by one dereference. p points to i, q points to p, and r points to q.

So *p reaches i directly; **q goes through p to i; and ***r goes through q and p to i. All three arrive at the same object and print 8. The distractors offer addresses, which would appear only if fewer dereferences were applied than there are levels of indirection.

Please log in or register to report this question.

9.

What is the output of this C code?

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

 

Answer: B

Correct answer: 11 11 Undefined-value

int **const p makes the parameter itself constant, but *p is not, so *p = &j is legal and the caller’s pointer is redirected to j. The first two prints give 11, one inside foo and one immediately after it returns. By the third, j’s lifetime has ended and the value is indeterminate - compare with the previous question, where the const sits in the other position and the code will not compile at all.

Please log in or register to report this question.

Answer: C

Correct answer: j is a pointer to an int and stores address of i

The declaration int *j = &i; declares j as a pointer to int and initialises it with the address of i.

The asterisk belongs to the declarator, not to the type specifier - the declaration reads "*j is an int", hence j is a pointer to int. i is an ordinary int and no pointer at all, and j has one level of indirection, not two, which the number of asterisks makes plain.

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