⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readints.c

📁 C和指针非常好的一本书.里面的有许多代码可以借鉴.
💻 C
字号:
/*
** Read an EOF-terminated list of integers from the standard input
** and return a dynamically allocated array containing the values.
** The first element of the array contains a count of the number
** of values it contains.
*/

#include <stdio.h>
#include <malloc.h>

#define	DELTA		100

int *
readints()
{
	int	*array;
	int	size;
	int	count;
	int	value;

	/*
	** Get the initial array, large enough to hold DELTA values.
	*/
	size = DELTA;
	array = malloc( ( size + 1 ) * sizeof( int ) );
	if( array == NULL )
		return NULL;

	/*
	** Get values from the standard input.
	*/
	count = 0;
	while( scanf( "%d", &value ) == 1 ){
		/*
		** Make the array bigger if needed, then store
		** the value.
		*/
		count += 1;
		if( count > size ){
			size += DELTA;
			array = realloc( array,
			    ( size + 1 ) * sizeof( int ) );
			if( array == NULL )
				return NULL;
		}
		array[ count ] = value;
	}

	/*
	** Resize the array to the exact size, then store the count
	** and return the array.  This never makes the array bigger
	** and so should never fail.  (Check it anyway!)
	*/
	if( count < size ){
		array = realloc( array,
		    ( count + 1 ) * sizeof( int ) );
		if( array == NULL )
			return NULL;
	}
	array[ 0 ] = count;
	return array;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -