C Primer Plus(第六版)12.9 编程练习 第8题

#include <stdio.h>

#include <stdlib.h>

int * make_array(int elem, int val);

void show_array(const int ar[], int n);

int main(void)

{

int * pa;

int size;

int value;

printf("Enter the number of elements:");

while(scanf("%d", &size) == 1 && size > 0)

{

printf("Enter the initialization value:");

scanf("%d", &value);

pa = make_array(size, value);

if(pa)

{

show_array(pa, size);

free(pa);

}

printf("Enter the number of elements (<1 to quit):");

}

printf("Done!");

return 0;

}

int * make_array(int elem, int val)

{

int*p;

int i;

p=malloc(elem*sizeof(int));

for(i=0;i<elem;i++)

*(p+i)=val;

return p;

}

void show_array(const int ar[], int n)

{

int i;

for(i=0;i<n;i++)

{

printf("pa[%d]=%d",i,ar[i]);

if((i+1)%8==0)

printf("\n");

}

}

相关推荐
biter down5 小时前
14:pytest-order 插件 顺序控制案例
开发语言·python·pytest
郝学胜-神的一滴6 小时前
Qt 高级开发 009: C++ Lambda 表达式
开发语言·c++·qt·软件构建
星栈独行6 小时前
我在 Rust 全栈项目里用 JWT 做无状态认证
开发语言·后端·rust·前端框架·开源·github·web
石山代码6 小时前
C++ 轻量级日志系统
开发语言·c++
小技与小术7 小时前
玩转Flask
开发语言·python·flask
SilentSamsara7 小时前
Python 性能优化:tracemalloc、profiling 与 C 扩展加速
开发语言·python·青少年编程·性能优化
冰小忆7 小时前
大驼峰命名规范和小驼峰命名规范的区别是什么?
开发语言·python
冉卓电子9 小时前
MPC5604B/C eMIOS 高级定时器全解
c语言
এ慕ོ冬℘゜9 小时前
JS 前端基础面试题
开发语言·前端·javascript
浩少7029 小时前
【无标题】
java·开发语言