Created
May 1, 2018 11:28
-
-
Save juliecious/f7d167d4bc65245b9f7ff6a313e1473a to your computer and use it in GitHub Desktop.
malloc()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main () { | |
int n=0; | |
printf("1.請輸入一為整數陣列的大小: "); | |
scanf("%d", &n); | |
int *ary = malloc(sizeof(int) * n); | |
printf("\n陣列位址\t\t陣列元素\t儲存值\n"); | |
int i; | |
for(i=0; i<n; i++) | |
printf("%p\t\tary[%d]\t\t%d\n", ary+i, i, *(ary+i)); | |
printf("\n2.設定陣列元素初值:\n"); | |
for(i=0; i<n; i++) { | |
printf("\tary[%d] = ", i); | |
scanf("%d", &ary[i]); | |
} | |
printf("\n3.顯示陣列元素設定值:\n"); | |
printf("\n陣列位址\t\t陣列元素\t儲存值\n"); | |
for(i=0; i<n; i++) | |
printf("%p\t\tary[%d]\t\t%d\n", &ary[i], i, ary[i]); | |
free(ary); | |
printf("\n...已釋放記憶體...\n"); | |
printf("\n\n4.顯示陣列元素設定值:\n"); | |
printf("\n陣列位址\t\t陣列元素\t儲存值\n"); | |
for(i=0; i<n; i++) | |
printf("%p\t\tary[%d]\t\t%d\n", &ary[i], i, ary[i]); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment