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 ...
随机推荐
- Struts2 自定义MVC框架
一.Model1与Model2: Model1:就是一种纯jsp开发技术,将业务逻辑代码和视图渲染代码杂糅在一起. Model2:Model2是在Model1的基础上,将业务逻辑的代码分离开来,单独形 ...
- LeetCode:Multiply Strings
题目链接 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
- python 列表 总结
在python里创建列表和字典非常简单,这里总结一下它们的常用方法 1.创建列表 myArry = ["one", "two", "three&quo ...
- 用机器名访问和用Localhost访问时在IE中的区别(备忘)
meta中未指定文档模式的时候, localhost访问文档模式默认是Edge 机器名访问时文档模式默认是IE7 <head>中添加 <meta http-equiv="X ...
- css 补漏
1.box-sizing: width(宽) + padding(内边距) + border(边框) = 元素实际宽度 height(高) + padding(内边距) + border(边框) ...
- 跳转Activity两种方法
摘要:假设从A界面开启另外一个B界面根据是否需要返回数据分为两种方式 一.无需返回数据方式 在A界面中调用startActivity方法进行直接跳转即可 二.需要返回数据方式 1.在A界面中调用sta ...
- BZOJ2242 [SDOI2011]计算器
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Select Top在七种数据库中的使用方法(包含mysql)
1. Oracle数据库 SELECT * FROM TABLE1 WHERE ROWNUM<=N 2. Infomix数据库 SELECT FIRST N * FROM TABLE1 3. D ...
- js,java,浮点数运算错误及应对方法
js,java浮点数运算错误及应对方法 一,浮点数为什么会有运算错误 IEEE 754 标准规定了计算机程序设计环境中的二进制和十进制的浮点数自述的交换.算术格式以及方法. 现有存储介质都是2进制.2 ...
- mogodb3.2源码安装
mogodb3.2源码安装 下载链接: http://www.mongodb.org/downloads 1.环境准备: 1.mkdir -p /data/tgz #创建存放软件的目录 2.mkdir ...