C和指针 (pointers on C)——第十一章:动态内存分配(下)习题

1、编写calloc,内部用malloc。

void *calloc (size_t n, size_t size)
{
	char * memory;
	memory =(char*) malloc(n * size);
	while( memory != NULL)
	{
		char * ptr;
		ptr = memory;
		while ( --n >= 0)
		{
			*ptr++ = '\0';
		}
	}
	return memory;

}
2、编写一个函数,动态存储一列输入的整数。

#include <stdlib.h>

int * readints()
{
	int *array;
	int value;
	int length = 1;
	array = (int *) malloc(length * sizeof(int));
	if (array == NULL)
	{
		return NULL;
	}
	while ( scanf_s("%d", &value) == 1)
	{
		length++;
		array =(int *) realloc(array,  length * sizeof(int));
		if (array == NULL)
		{
			return NULL;
		}
		array[length-1] = value;
	}
}

3、编写一个函数,动态存储一列输入的char。


#include "stdlib.h"

char * readstring()
{
	char *array;
	char *ptr;
	int length = 1;
	array = (char *) malloc (sizeof(char));
	gets(ptr);
	if (ptr == NULL)
	{
		exit (EXIT_FAILURE);
	}
	while (*ptr != NULL )
	{
		length++;
		array  = (char *) realloc (array , length * sizeof(char));
		if (array == NULL)
			exit (EXIT_FAILURE);
		array[length - 1] = *ptr;
		ptr++;
	}
	//追加一个NUL
	array  = (char *) realloc (array , (length++) * sizeof(char));
	if (array == NULL)
		exit (EXIT_FAILURE);
	array[length - 1] = '\0';
	return array;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Contents A Quick Start ........................................................................................................ 1 Basic Concepts ...................................................................................................... 7 Data ....................................................................................................................... 11 Statements ............................................................................................................. 15 Operators and Expressions .................................................................................... 23 Pointers .................................................................................................................. 29 Functions ............................................................................................................... 37 Arrays .................................................................................................................... 43 Strings, Characters, and Bytes .............................................................................. 55 Structures and Unions ........................................................................................... 69 Dynamic Memory Allocation ................................................................................ 75 Using Structures and Pointers ............................................................................... 79 Advanced Pointer Topics ...................................................................................... 87 The Preprocessor ................................................................................................... 93 Input/Output Functions .......................................................................................... 95 Standard Library .................................................................................................... 119 Classic Abstract Data Types ................................................................................. 129 Runtime Environment ........................................................................................... 145
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值