Pointers

GATE CS previous year questions · 5 questions

GATE CS 2025 Set 1 | Question 34

NAT · 1 mark
#include <stdio.h>
void foo(int *p, int x){
    *p=x;
}
int main(){
    int *z;
    int a = 20, b = 25;
    z = &a;
    foo(z,b);
    printf("%d",a);
    return 0;
}

The output of the given C program is __________. (Answer in integer)

Explanation

Answer: 25

In main, z = &a makes z hold the address of a. The call foo(z,b) copies that address into p and the value 25 into x.

Inside foo, *p=x stores 25 at the address held by p, which is the variable a itself. So a changes from 20 to 25, and printf prints 25.

Answer: 25

GATE CS 2025 Set 2 | Question 19

MCQ · 1 mark · −0.33

Consider the following C program:

#include <stdio.h>
void stringcopy(char *, char *);
int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
}
void stringcopy(char *s, char *t) {
    while(*t)
        *s++ = *t++;
}

Which ONE of the following will be the output of the program?

Options for GATE CS 2025 Set 2 | Question 19

Explanation

Answer: (D)

The array holds the 14 characters @#Hello World! at indices 0 to 13, followed by '\0' at index 14.

stringcopy(a, a + 2) copies one character at a time from t to s, with t always two places ahead of s. Every character is therefore read before it can be overwritten, and the 12 characters Hello World! land in indices 0 to 11.

The loop stops as soon as *t is '\0', so the terminator is never copied. Indices 12 and 13 keep their old characters d!, and the string still ends at index 14, so printf prints Hello World!d!.

Answer: D

GATE CS 2025 Set 2 | Question 62

NAT · 2 marks

Consider the following C program:

#include <stdio.h>
int main(){
    int a;
    int arr[5] = {30,50,10};
    int *ptr;
    ptr = &arr[0] + 1;
    a = *ptr;
    (*ptr)++;
    ptr++;
    printf("%d", a + (*ptr) + arr[1]);
    return 0;
}

The output of the above program is ___________. (Answer in integer)

Explanation

Answer: 111

  • The array holds {30, 50, 10, 0, 0}: elements without an initialiser become 0.
  • ptr = &arr[0] + 1 points to arr[1], so a = 50.
  • (*ptr)++ increments the element, not the pointer, so arr[1] becomes 51. The copy in a stays 50.
  • ptr++ moves the pointer to arr[2], so *ptr is 10.

The program prints \(50 + 10 + 51 = 111\).

Answer: 111

GATE CS 2024 Set 2 | Question 33

MSQ · 1 mark

Consider the following C function definition.

int fX(char *a){
    char *b = a;
    while(*b)
        b++;
    return b - a;}

Which of the following statements is/are TRUE?

Options for GATE CS 2024 Set 2 | Question 33

Explanation

Answer: (A), (B), (D)

fX moves the pointer b forward until it reaches the terminating '\0' and returns b - a, the number of characters before it: the length of the string. The pointer difference is converted to int on return, so the function is valid C and (C) is false.

  • (A) A string literal is always stored with a terminating null character, so the call returns 4.
  • (B) char c[] = "abcd" creates a 5-element array whose last element is '\0', so the call returns 4.
  • (D) c points to the literal itself, which also ends in '\0', so the call returns 4.

The loop stops in every case, so all three calls return a value.

Answer: A, B, D

GATE CS 2024 Set 2 | Question 36

MCQ · 2 marks · −0.66

What is the output of the following C program?

#include <stdio.h>
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;}
Options for GATE CS 2024 Set 2 | Question 36

Explanation

Answer: (B)

p points to a[0], and q = p + 1 points to the next element, a[1].

  • Subtracting two pointers into the same array counts elements, not bytes, so q - p is 1, even though a double usually takes 8 bytes.
  • *q - *p subtracts the stored values: \(25.0 - 20.0 = 5.0\), which the cast turns into the int 5.

So the program prints 1,5. Option (C) reports the pointer gap in bytes instead of elements, and the 4 and 8 in (A) and (D) match neither expression.

Answer: B