如何编写读取并修改用户创建的动态数组的函数?是否可行?
Absolutely you can do both—let’s break this down with practical examples (we’ll use C since it’s the most common language for manual dynamic array implementation, but the logic applies to other languages with pointer/reference support).
First, remember that a dynamic array is just a pointer to allocated memory plus a length (since we don’t have built-in bounds checking). To read it, your function just needs access to the pointer (array start) and the number of elements.
Here’s a complete example:
#include <stdio.h> #include <stdlib.h> // Function to read and print the dynamic array void readDynamicArray(int *arr, int length) { printf("Current array elements: "); for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int length; printf("Enter the length of your dynamic array: "); scanf("%d", &length); // Allocate memory for the array int *userArray = (int *)malloc(length * sizeof(int)); if (userArray == NULL) { // Always check for allocation failure! printf("Error: Couldn't allocate memory.\n"); return 1; } // Let the user input values printf("Enter %d integers: ", length); for (int i = 0; i < length; i++) { scanf("%d", &userArray[i]); } // Call our read function readDynamicArray(userArray, length); // Clean up allocated memory free(userArray); return 0; }
This function works by iterating over the memory pointed to by arr and printing each element—simple and straightforward.
Yes! Since we’re passing the pointer to the array (not a copy of the array), any changes we make inside the function directly affect the original memory. You can even combine reading and modifying logic in the same function.
Example 1: Modify all elements (e.g., double their values)
// Function that modifies elements and reads/prints the result void modifyAndReadArray(int *arr, int length) { // Modify each element for (int i = 0; i < length; i++) { arr[i] *= 2; } // Read/print the modified array printf("After doubling all elements: "); for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } printf("\n"); }
Add this to the main function after reading the initial array:
modifyAndReadArray(userArray, length); // Verify the changes with our original read function readDynamicArray(userArray, length);
Example 2: Let the user modify a specific element
// Function to let the user update a chosen element void userModifyElement(int *arr, int length) { int index, newValue; printf("\nEnter the index of the element to modify (0 to %d): ", length - 1); scanf("%d", &index); if (index >= 0 && index < length) { printf("Enter the new value: "); scanf("%d", &newValue); arr[index] = newValue; // Directly modifies the original array printf("Element at index %d updated to %d\n", index, newValue); // Read the updated array right away readDynamicArray(arr, length); } else { printf("Invalid index—please try again.\n"); } }
Call this in main too, and you’ll see the original array changes instantly.
- Always check if
mallocreturnsNULL—this happens when there’s not enough memory available. - Never forget to
free()the dynamic array when you’re done with it to avoid memory leaks. - In C++, you could use
std::vectorwhich handles memory management automatically, but the same pointer/reference logic applies if you want to write custom functions for it.
Hope this makes it clear—dynamic arrays are designed to be flexible for both reading and modifying once you understand how pointer-based memory access works!
内容的提问来源于stack exchange,提问作者Jeremy S Nero

