All Exams Test series for 1 year @ ₹349 only
Question

Only legal pointer operations:
A. pointer + number $\rightarrow$ pointer
B. pointer - number $\rightarrow$ number
C. pointer + pointer $\rightarrow$ pointer
D. pointer - pointer $\rightarrow$ pointer
E. pointer - pointer $\rightarrow$ number
Choose the most appropriate answer from the options given below:

The correct answer is
A, E Only

Understanding Legal Pointer Operations

Pointer arithmetic in C and C++ refers to the operations that can be performed on pointers. These operations allow you to move pointers in memory, typically to access elements within arrays. It's crucial to understand which operations are permitted and what their results mean to avoid undefined behavior and memory errors.

Rules of Pointer Arithmetic

Pointer arithmetic is generally defined for pointers that point to elements within the same array or one position past the end of the array. The basic rules are:

  • Adding or subtracting an integer to/from a pointer results in a pointer.
  • Subtracting two pointers (that point to elements in the same array) results in an integer.

Operation 1: Pointer + Integer

Adding an integer value, say $n$, to a pointer $p$ is a legal operation. The result is a new pointer that points $n$ elements forward in memory from the location pointed to by $p$. For this to be meaningful, $p$ usually points to an element of an array.

Format: $pointer + number$

Result Type: $pointer$

LaTeX Representation: $p + n \rightarrow \text{pointer}$

Operation 2: Pointer - Integer

Subtracting an integer value, say $n$, from a pointer $p$ is also a legal operation. The result is a new pointer that points $n$ elements backward in memory from the location pointed to by $p$. Again, this is typically used within the bounds of an array.

Format: $pointer - number$

Result Type: $pointer$

LaTeX Representation: $p - n \rightarrow \text{pointer}$

Operation 3: Pointer - Pointer

Subtracting one pointer from another is legal only if both pointers point to elements within the same array (or one points one position past the end). The result is not a pointer but an integer value representing the number of elements between the two pointers.

Format: $pointer - pointer$

Result Type: $number (integer)$

LaTeX Representation: $p_1 - p_2 \rightarrow \text{number (integer)}$

Illegal Pointer Operations

Certain pointer arithmetic operations are not allowed in C/C++:

  • Adding two pointers ($pointer + pointer$) is illegal. You cannot add two memory addresses together.
  • Multiplying, dividing, or taking the modulus involving pointers or between pointers and integers is generally not allowed or doesn't have a defined meaning in standard pointer arithmetic.

Analyzing the Given Options

Let's evaluate each option based on the rules of pointer arithmetic:

Option Operation Result Type Legality Explanation
A pointer + number pointer Legal Adds an integer offset to a pointer, resulting in a new pointer. Matches rule 1.
B pointer - number number Illegal (as described) The operation $pointer - number$ is legal, but its result is a pointer, not a number.
C pointer + pointer pointer Illegal Adding two memory addresses is not permitted.
D pointer - pointer pointer Illegal (as described) Subtracting two pointers yields an integer (number of elements), not a pointer.
E pointer - pointer number Legal Subtracting two pointers (within the same array) yields the count of elements between them. Matches rule 3.
Analysis of Pointer Arithmetic Operations

Conclusion on Valid Pointer Operations

Based on the analysis, the operations described in options A and E are the only ones listed that represent legal pointer arithmetic operations with the correct resulting types.

  • Option A ($pointer + number → pointer$) is correct.
  • Option E ($pointer - pointer → number$) is correct.

Therefore, the combination representing only legal pointer operations among the choices is A and E.

Was this answer helpful?

Important Questions from Pointer

  1. What would be the equivalent pointer expression for referring the array element ar[m][n][o]
  2. 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?

  3. 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)

  4. Consider the following ANSI-C program.

    #include <stdio.h>
    int main(){
     int *ptr, a, b, c;
     a=5; b=11; c=20;
     ptr=&a; *ptr=c; ptr=&c;
     a=*(&b); c=*ptr-a;
     printf("%d",c);
     return(0);
    }

    The output of this program is ____________. (answer in integer)

    Note: Assume that the program compiles and runs successfully.

  5. The following statement in ‘C’
    int (*f())[ ];
    declares
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App