invcreat.c

来自「与C语言四书五经之《C和指针》的配套的书上源代码。」· C语言 代码 · 共 50 行

C
50
字号
/*
** Function to create a SUBASSEMBLY inventory record.
*/

#include <stdlib.h>
#include <stdio.h>
#include "inventor.h"

Invrec *
create_subassy_record( int n_parts )
{
	Invrec	*new_rec;

	/*
	** Try to get memory for the Invrec portion.
	*/
	new_rec = malloc( sizeof( Invrec ) );
	if( new_rec != NULL ){
		/*
		** That worked; now get the SUBASSYINFO portion.
		*/
		new_rec->info.subassy =
		    malloc( sizeof( Subassyinfo ) );
		if( new_rec->info.subassy != NULL ){
			/*
			** Get an array big enough for the parts.
			*/
			new_rec->info.subassy->part = malloc(
			    n_parts * sizeof( struct SUBASSYPART ) );
			if( new_rec->info.subassy->part != NULL ){
				/*
				** Got the memory; fill in the fields
				** whose values we know and return.
				*/
				new_rec->type = SUBASSY;
				new_rec->info.subassy->n_parts =
				    n_parts;
				return new_rec;
			}

			/*
			** Out of memory: free what we've got so far.
			*/
			free( new_rec->info.subassy );
		}
		free( new_rec );
	}
	return NULL;
}

⌨️ 快捷键说明

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