malloc和calloc用法
程序员文章站
2022-06-27 22:05:28
malloc和calloc用法 c include include int main(){ int n; printf("input n: "); scanf("%d", &n); //一个参数,指定具体空间的大小 int p = (int )malloc(sizeof(int) n); if(NU ......
malloc和calloc用法
#include <stdio.h> #include <stdlib.h> int main(){ int n; printf("input n:>"); scanf("%d", &n); //一个参数,指定具体空间的大小 int *p = (int*)malloc(sizeof(int) * n); if(NULL == p){ } //两个参数,第一个参数是个数,第二个参数是每个的大小 int *u = (int*)calloc(n, sizeof(int)); for(int i = 0; i < n; ++i){ *(p + i) = i; } for(int i = 0; i < n; ++i){ printf("%d ", *(p + i)); } printf("\n"); for(int i = 0; i < n; ++i){ *(u + i) = i; } for(int i = 0; i < n; ++i){ printf("%d ", *(u + i)); } printf("\n"); free(p); free(u);