C和指针 第十一章 习题
1编写calloc,内部使用malloc函数获取内存
#include <stdio.h>
#include <stdlib.h> void *myAlloc(unsigned long int length, unsigned long int typeSize)
{
int *ptr;
int index = 0;
int totalLen = length * typeSize;
if(length >= 0 && typeSize >= 0){
//返回后需要类型转换一下,不可以对void *类型直接取值。
ptr = (int*)malloc(totalLen);
if(ptr != NULL){
for(index = 0; index < totalLen; index++){
*(ptr + index) = 0;
}
return ptr;
}
return NULL;
} return NULL;
} int main()
{
int *ptr = myAlloc(10, sizeof(int));
int index;
for(index = 0; index < 10; index++){
printf("%d\t", *(ptr + index));
}
}
运行

2.编写函数从标准输入读取一列整数,把这些值存储于一个动态分配的数组中,并返回数组,函数通过EOF判断输入结束,数组第一个元素表示数组长度。
#include <stdio.h>
#include <stdlib.h> int *getInputToArray()
{
int *array;
int count = 0;
int num; array = malloc(1);
array[0] = count;
while(scanf("%d", &num) != EOF){
count++;
array = realloc(array, (count + 1)* sizeof(int));
array[count] = num;
array[0] = count;
} return array;
} int main()
{
int *arr = getInputToArray();
printf("%d\n", arr[0]); return 0;
}
运行输入ctrl+D结束符EOF

3.编写函数从标注输入中读取字符串,然后把字符串复制到动态分配的内存中,并返回该字符串的拷贝,不应该对输入长度做限制。
#include <stdio.h>
#include <string.h>
#include <stdlib.h> char *getInputStringy()
{
char *str = malloc(1);
char *tmp;
//加上末尾的'\0',初始的length应该为1
int length = 1;
char ch; while((ch = getchar()) != EOF){
length++;
tmp = realloc(str, length * sizeof(char));
if(tmp != NULL){
//保存输入的字符
strcpy(tmp, str);
tmp[length - 2] = ch;
tmp[length - 1] = '\0';
}else{
return NULL;
}
str = tmp;
}
return str;
} int main()
{ char *str = getInputStringy();
printf("%s", str); return 0;
}
运行:ctrl+D停止输入

4.编写一个链表
#include <stdio.h>
#include <stdlib.h> typedef struct node {
//指向下一个结构体的指针
struct node *next;
int value;
} LinkList; int main()
{
LinkList third = {NULL, 3};
LinkList second = {&third, 2};
LinkList first = {&second, 1};
struct node *ptr = &first;; while(ptr != NULL){
printf("%d\t", ptr -> value);
ptr = ptr -> next; }
return 0;
}
运行:

C和指针 第十一章 习题的更多相关文章
- C和指针 第六章 习题
6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...
- C和指针 第十七章 习题
17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...
- C和指针 第十三章 习题
1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...
- C和指针 第十一章 动态内存分配
声明数组时,必须指定数组长度,才可以编译,但是如果需要在运行时,指定数组的长度的话,那么就需要动态的分配内存. C函数库stdlib.h提供了两个函数,malloc和free,分别用于执行动态内存分配 ...
- C和指针 第七章 习题
7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...
- java编程思想第四版第十一章习题
第一题 package net.mindview.holding.test1; import java.util.ArrayList; import java.util.List; /** * 沙鼠 ...
- C和指针 第五章 习题
下列输出的值: #include <stdio.h> int func(){ static int count = 1; return ++count; } int main() { in ...
- C和指针 第四章 习题
4.1正数的n的平方根可以通过: ai+1= (ai + n / ai ) / 2 得到,第一个a1是1,结果会越来越精确. #include <stdio.h> int main() { ...
- C和指针 第三章 习题
在一个源文件中,有两个函数x和y,定义一个链接属性external储存类型static的变量a,且y可以访问,x不可以访问,该如何定义呢? #include <stdio.h> void ...
随机推荐
- 洛谷P1462 通往奥格瑞玛的道路[二分答案 spfa 离散化]
题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...
- C 语言学习 第三次作业总结
本次作业内容: For循环的使用 If判断语句的使用 常用数学运算表达式的使用 数学函数库中几个常见函数的使用及自我实现 将操作代码提交到coding 作业总结: For循环是C语言中一种基本的循环语 ...
- java8日期时间
###与时间有关的5个包* java.time* java.time.chrono* java.time.format* java.time.temporal* java.time.zone###ja ...
- [转] form.getForm().submit的用法及Ext.Ajax.request的小小区别
原文地址:http://blog.csdn.net/hongleidy5000/article/details/7329325 if (!formDetail.getForm().isValid()) ...
- 用 CSS 隐藏页面元素
用 CSS 隐藏页面元素有许多种方法.你可以将 opacity 设为 0 将 visibility 设为 hidden 将 display 设为 none 将 position 设为 absolute ...
- jquery 将disabled的元素置为enabled的三种方法
转--http://www.jb51.net/article/19342.htm 在jquery中可以通过jqueryObj.attr("disabled","disab ...
- 解决:Error: JAVA_HOME is not defined correctly
问题重现: Error: JAVA_HOME is not defined correctly. We cannot execute :/usr/lib/jvm/java-7-oracle 问题分析: ...
- Outlook2016 新装进阶操作指南
启动图片自动下载 键盘上同时按下WIN+R,弹出运行输入框,输入outlook,回车后打开Outlook 依次点击左上角文件,选项,信任中心,信任中心设置,自动下载 在窗口右边反勾选"在HT ...
- linux下centos的网络连接
- 理解DOM
http://www.cnblogs.com/chaogex/p/3959723.html 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言 ...