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. es6学习笔记1 --let以及const

    let语句的基本用法:  1.let声明的变量为块级作用域,只在最近的{}里面有效,如果在外部引用就会报错. { let a = 10; var b = "hello" } ale ...

  2. flush和reflush

    /** * jpa 中的reflush 同 hibernate 中 Session 的 refresh 方法. * * reflush 会强制发送sql查询(select)语句,使缓存中的数据和数据库 ...

  3. 第10章 Java类的三大特性之一:多态

    1.Java中的多态 多态是指对象的多种形态,主要包括这两种: 1.1引用多态 a.父类的引用可以指向本类的对象b.父类的引用可以指向子类的对象举个例子:父类Anmail,子类Dog,可以使用父类An ...

  4. PHP用法

    链接: php编写app接口(一)-JSON方式封装接口数据方法 php 非常有用的高级函数PATH_SEPARATOR常量和set_include_path date_default_timezon ...

  5. 1089 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa ...

  6. Openjudge 1.13.37:乒乓球

    总时间限制:  1000ms 内存限制:  65536kB 描述 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员 ...

  7. 栈的理解以及如何计算程序所需栈的大小并在IAR中设置栈

    文章首发于浩瀚先森博客 #栈的理解 一个程序大体上讲都是由变量和函数组合而成,变量有全局变量和局部变量,还有函数间传值的参数以及返回值. Stack是为了程序运行过程中临时保存所需数据而在内存里分配的 ...

  8. Android开发自学笔记(Android Studio)—4.4 AdapterView及其子类

    一.引言       AdapterView本身是一个抽象类,而它派生的子类在用法上也基本相似,只是在显示上有一定区别,因此把他们也归为一类.       AdapterView具有如下特征: Ada ...

  9. gdb调试常用实用命令和core dump文件的生成

      1.生成core dump文件的方法: $  ulimit -c //查看是否为0 如果为0 $   ulimit -c unlimited 这样在程序崩溃以后会在当前目录生成一个core.xxx ...

  10. lvs/dr配置

    lvs/dr Director server : DIP:192.168.1.100/24  eth0 VIP:192.168.1.101/24  eth0:0 Real server: Real1: ...