运行 ❯
获取你的
自己的
网站
×
更改纵向/横向
更改主题,深色/浅色
前往 Spaces
#include <stdio.h> #include <stdlib.h> int main() { // Allocate memory for a number of items int numItems = 10; int *myArray = malloc(numItems * sizeof(int)); // Write into the memory for(int i = 0; i < numItems; i++) { myArray[i] = i + 1; } // Reallocate the memory numItems = 20; myArray = realloc(myArray, numItems * sizeof(int)); // Display the contents of the memory for(int i = 0; i < numItems; i++) { printf("%d ", myArray[i]); } // Free the memory free(myArray); myArray = NULL; return 0; }
1 2 3 4 5 6 7 8 9 10 134465 0 0 0 0 0 0 0 0 0