You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言:判断指针是否在数组内及实现指定打印功能的技术咨询

Solution to Print Elements After Pointer x in C (No Local Variables, No Array Subscripts)

Hey Anna, great job getting the pointer validity check right—your condition x < (arr + n) && x >= arr is spot-on for verifying that x points to an element within arr. Now let's tackle the rest of the problem while respecting the constraints: no local variables, no array subscripts.

Core Approach

Since we can't declare new local variables, we'll work directly with the input pointers we already have. We'll use pointer arithmetic exclusively to traverse the array, and avoid any array subscript ([]) syntax entirely.

Loop-Based Implementation

This is the most straightforward approach. We'll adjust the input pointer x to point to the element right after the target, then iterate until we reach the end of the array. Modifying the input parameter is allowed here (the problem doesn't restrict this), and we won't create any new variables:

#include <stdio.h>

void printAfterX(int* arr, int n, int* x) {
    if (x < (arr + n) && x >= arr) {
        // Move pointer to the element immediately after x
        x++;
        // Iterate until we pass the last array element
        for (; x < arr + n; x++) {
            // Dereference pointer to print the value
            printf("%d ", *x);
        }
        printf("\n"); // Optional: clean up output with a newline
    }
}

Recursive Alternative

If you prefer a recursive style (which also avoids local variables), we can leverage the input pointers to drive the recursion. Each call prints the current element and recurses with the incremented pointer:

#include <stdio.h>

void printAfterX(int* arr, int n, int* x) {
    if (x < (arr + n) && x >= arr) {
        // Shift to the element after x first
        x++;
        if (x < arr + n) {
            printf("%d ", *x);
            // Recurse with the updated pointer
            printAfterX(arr, n, x);
        } else {
            printf("\n");
        }
    }
}

Key Details to Confirm

  • No local variables: Both implementations only use the input parameters provided—no new variables are declared.
  • No array subscripts: All array access uses pointer dereferencing (*x) and arithmetic (arr + n), which complies with the constraint.
  • Correct behavior: Both start printing from the element immediately after x and continue until the end of the array.

内容的提问来源于stack exchange,提问作者Anna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:39:46