190 views
5 5 votes

Consider the following declarations:

#include <stdio.h>

int main() {
    int a[4] = {6, 4, 1, 2};
    int b[8] = {9, 8, 11, 10, 5, 7, 0, 3};

    int *p = &a[1];
    int *q = b;

    printf("%p", p + q);

    return 0;
}

What will happen?

  1. It prints the sum of two addresses
     
  2. It prints the address of $\texttt{a[1] + b[0]}$
     
  3. Compilation error
     
  4. Undefined behavior after successful compilation

1 Answer

3 3 votes

In C, adding two pointers is not allowed.

This expression is invalid:

$\texttt{p + q}$

Pointer arithmetic allows adding an integer to a pointer, like:

$\texttt{p + 1}$

But adding one pointer to another pointer is not valid.

So,

$\texttt{p + q}$

will cause a compilation error.

Therefore, the correct answer is $\texttt{Compilation error}$.

Answer: C

Answer:
Position:
Show:

Related questions

6 6 votes
1 1 answer
232
232 views
GO Classes asked Jun 23
232 views
What is the output of the following code?#include <stdio.h int main() { char str[] = "CODE"; char *p = str; p++; *p = 'A'; printf("%s %c", str, *(p + 2)); return 0; }$\te...
7 7 votes
2 2 answers
245
245 views
GO Classes asked Jun 23
245 views
What is the output of the following code?#include <stdio.h int main() { int a[] = {5, 10, 15, 20}; int *p = a + 1; printf("%d %d %td", *(p + 1), p , (p + 2) - a); return ...
6 6 votes
1 1 answer
208
208 views
GO Classes asked Jun 23
208 views
What is the output of the following code?#include <stdio.h int main() { char str[] = "GATE"; char *p = str; printf("%c %c %s", str , *(p + 2), p + 1); return 0; }$\texttt...
6 6 votes
1 1 answer
196
196 views
GO Classes asked Jun 23
196 views
What will happen when the following code is compiled?#include <stdio.h int main() { int a[] = {10, 20, 30}; a = a + 1; printf("%d", *a); return 0; }$\texttt{10}$ $\texttt...