如何不使用额外变量将用户输入字符串分配至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
scanffunction with the%msspecifier (C11 and later): This tellsscanfto automatically allocate enough memory for the input string and store the resulting pointer directly in the providedchar*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
- Array Allocation: We directly allocate the
char**array withmalloc—no extra variable to hold the size or temp pointer; we compute the required size inline. - Reading Input: Each
scanf("%ms", arr + n)reads a string intoarr[n]. The logical AND (&&) ensures we stop processing if anyscanfcall 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.). - 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
%msspecifier 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 usegetline, but that requires asize_t*parameter—which would force you to use a variable (breaking the "no extra variables" rule). So%msis 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
相关产品推荐
相关产品推荐

