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和指针 第十一章 习题的更多相关文章

  1. C和指针 第六章 习题

    6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...

  2. C和指针 第十七章 习题

    17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...

  3. C和指针 第十三章 习题

    1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...

  4. C和指针 第十一章 动态内存分配

    声明数组时,必须指定数组长度,才可以编译,但是如果需要在运行时,指定数组的长度的话,那么就需要动态的分配内存. C函数库stdlib.h提供了两个函数,malloc和free,分别用于执行动态内存分配 ...

  5. C和指针 第七章 习题

    7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...

  6. java编程思想第四版第十一章习题

    第一题 package net.mindview.holding.test1; import java.util.ArrayList; import java.util.List; /** * 沙鼠 ...

  7. C和指针 第五章 习题

    下列输出的值: #include <stdio.h> int func(){ static int count = 1; return ++count; } int main() { in ...

  8. C和指针 第四章 习题

    4.1正数的n的平方根可以通过: ai+1= (ai + n / ai ) / 2 得到,第一个a1是1,结果会越来越精确. #include <stdio.h> int main() { ...

  9. C和指针 第三章 习题

    在一个源文件中,有两个函数x和y,定义一个链接属性external储存类型static的变量a,且y可以访问,x不可以访问,该如何定义呢? #include <stdio.h> void ...

随机推荐

  1. Android(Linux)控制GPIO的方法及实时性分析

    Linux下控制GPIO的方法有N种,详细请参考<RPi GPIO Code Samples>,文中用十多种语言演示了如何控制GPIO,非常全面详尽.因此,这里不再多做赘述,仅把调试过程中 ...

  2. USACO2.4 The Tamworth Two[模拟]

    题目描述 两只牛逃跑到了森林里.农夫John开始用他的专家技术追捕这两头牛.你的任务是模拟他们的行为(牛和John). 追击在10x10的平面网格内进行.一个格子可以是: 一个障碍物, 两头牛(它们总 ...

  3. etl结合java的例子

    1.新建Java测试类,导出Jar包,放在kettle目录中的libext文件中 package test; public class Test{ public static final String ...

  4. 嵌入式Linux驱动学习之路(二十三)NAND FLASH驱动程序

    NAND FLASH是一个存储芯片. 在芯片上的DATA0-DATA7上既能传输数据也能传输地址. 当ALE为高电平时传输的是地址. 当CLE为高电平时传输的是命令. 当ALE和CLE都为低电平时传输 ...

  5. Map接口,Map.Entry,hashMap类,TreeMap类,WeakHashMap。

    Collection接口之前接触过,每次保存的对象是一个对象,但是在map中保存的是一对对象,是以key->value形式保存的. 定义: public interface Map<K,V ...

  6. ThreadLocal实现方式&使用介绍—无锁化线程封闭

    原文出处: xieyu_zy 虽然现在可以说很多程序员会用ThreadLocal,但是我相信大多数程序员还不知道ThreadLocal,而使用ThreadLocal的程序员大多只是知道其然而不知其所以 ...

  7. Spring源码分析——BeanFactory体系之抽象类、类分析(一)

    上一篇介绍了BeanFactory体系的所有接口——Spring源码分析——BeanFactory体系之接口详细分析,本篇就接着介绍BeanFactory体系的抽象类和接口. 一.BeanFactor ...

  8. Ubuntu 16.04 LTS设置国内更新源

    ubuntu一般多用于开发环境,centos/redhat多用于企业环境.suse多用于银行金融行业!!! 01.ubuntu源地址 /etc/apt/sources.list 02.更新缓存资源索引 ...

  9. 使用css3 实现太阳升起落下效果

    <!DOCTYPE html><html style="overflow: hidden"><head> <meta http-equiv ...

  10. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...