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

如何不使用额外变量将用户输入字符串分配至char**数组?是否可行?

Is it possible to read terminal input into a char** array without using any extra variables?

Absolutely feasible! Let me break down how to do this in C, with zero extra variables beyond the char** array itself (since that's the target structure we're populating).

Key Tools We'll Use

  • The scanf function with the %ms specifier (C11 and later): This tells scanf to automatically allocate enough memory for the input string and store the resulting pointer directly in the provided char* location—no need for temp variables to hold buffer sizes or intermediate pointers.
  • C's comma operator: Lets us sequence expressions (run one operation, then another) and return the result of the final expression, which is perfect for chaining operations without extra variables.
  • Short-circuit logical operators (&&): Let us stop execution early if any input operation fails (like hitting EOF).

Full Implementation

#include <stdio.h>
#include <stdlib.h>

int main() {
    // Allocate a char** array to hold 4 strings (no extra variables here)
    char** arr = malloc(4 * sizeof(char*));
    
    // Read strings into each array index without extra variables
    // Each scanf writes directly to arr[n], && stops if any read fails
    (scanf("%ms", arr) && scanf("%ms", arr + 1) && scanf("%ms", arr + 2) && scanf("%ms", arr + 3));
    
    // Print and free the strings without loop variables or temp storage
    // Use nested ternary + comma operators to check each pointer, print, then free
    arr[0] ? (printf("%s\n", arr[0]), free(arr[0]), 
              arr[1] ? (printf("%s\n", arr[1]), free(arr[1]), 
                        arr[2] ? (printf("%s\n", arr[2]), free(arr[2]), 
                                  arr[3] ? (printf("%s\n", arr[3]), free(arr[3]), 0) : 0) : 0) : 0;
    
    free(arr);
    return 0;
}

How It Works

  1. Array Allocation: We directly allocate the char** array with malloc—no extra variable to hold the size or temp pointer; we compute the required size inline.
  2. Reading Input: Each scanf("%ms", arr + n) reads a string into arr[n]. The logical AND (&&) ensures we stop processing if any scanf call fails (e.g., if the user enters EOF). We don't need any variables to track the index—we use direct offset calculations (arr + 1, arr + 2, etc.).
  3. Cleanup & Output: Using nested ternary operators and comma sequences, we check if each pointer is non-NULL (to avoid printing invalid memory), print the string, free the allocated memory, and move to the next index. No loop variables or temp storage needed here either.

Notes

  • The %ms specifier is standard in C11 and supported by most modern compilers (GCC, Clang, MSVC). If you're working with an older compiler, you'd need to use getline, but that requires a size_t* parameter—which would force you to use a variable (breaking the "no extra variables" rule). So %ms is the cleanest path here.
  • If you need a dynamic number of strings (not a fixed 4), you could use recursion with the array pointer and current index—but that would require passing the index as part of the recursive call, which doesn't use extra variables (since it's a function parameter, not a temp variable in the main scope).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 14:47:53