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");

}

}

相关推荐
序属秋秋秋1 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
quant_19863 小时前
R语言如何接入实时行情接口
开发语言·经验分享·笔记·python·websocket·金融·r语言
啟明起鸣6 小时前
【网络编程】简易的 p2p 模型,实现两台虚拟机之间的简单点对点通信,并以小见大观察 TCP 协议的具体运行
c语言·网络·tcp/ip·p2p
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
风吹落叶花飘荡7 小时前
2025 Next.js项目提前编译并在服务器
服务器·开发语言·javascript
失败又激情的man8 小时前
python之requests库解析
开发语言·爬虫·python
专注VB编程开发20年8 小时前
常见 HTTP 方法的成功状态码200,204,202,201
开发语言·网络协议·tcp/ip·http
有没有没有重复的名字8 小时前
线程安全的单例模式与读者写者问题
java·开发语言·单例模式
张先shen8 小时前
Elasticsearch RESTful API入门:基础搜索与查询DSL
大数据·spring boot·elasticsearch·搜索引擎·全文检索·restful