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 ...
随机推荐
- Java反射特性--获取其他类实例并调用其方法
1. 代码结构 .├── com│ └── test│ └── MyTest.java└── MainCall.java 2. 代码内容 MyTest.java: package com.te ...
- 【C#】【Thread】CountdownEvent任务并行
System.Threading.CountdownEvent 是一个同步基元,它在收到一定次数的信号之后,将会解除对其等待线程的锁定. CountdownEvent 专门用于以下情况:您必须使用 ...
- SharePoint 2013技巧分享系列 - 同步Exchange显示高清用户照片
在“SharePoint 2013技巧分享系列 - Active Directory同步显示用户照片”文中介绍了如何同步Active Directory显示用户照片,但是同步完成后,用户照片尺寸和清晰 ...
- rsyslog及logrotate小结
[root@node1 logrotate.d]# ls dracut haproxy httpd mcelog nginx ppp psacct syslog yum yum install n ...
- input 禁止输入法
<INPUT TYPE = text STYLE = "ime-mode:disabled" > 即可禁止输入法 js形式: active 代表输入法为中文inacti ...
- [CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符
1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot us ...
- 重新注册IIS
出现问题的原因是先装了.NET4.0,再装IIS造成 处理方法:管理员权限执行cmd, C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_reg ...
- 【BZOJ 4561】【JLOI 2016】圆的异或并
http://www.lydsy.com/JudgeOnline/problem.php?id=4561 一开始并不会做,后来看题解看懂了. 看懂了之后还是错了好几次,数组大小手残开小了. 圆的包含并 ...
- 30秒懂SQL中的join(2幅图+30秒)
废话不多说,直接上图秒懂. t1表的结构与数据如下: t2表的结构与数据如下: inner join select * from t1 inner join t2 on t1.id = t2.id; ...
- git 代码更新
第一:先说首次使用 意思就是这个文件夹中的代码你还没有向GITHUB提交过代码 cd /home/test(假如 test就是你的用户名)/githubtest(这是个文件夹,你可以提前先建立好,这个 ...