Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!rochester!PT.CS.CMU.EDU!IUS1.CS.CMU.EDU!edw From: edw@IUS1.CS.CMU.EDU (Eddie Wyatt) Newsgroups: comp.lang.c Subject: Re: how to write a function that returns a string of N nulls Message-ID: <206@PT.CS.CMU.EDU> Date: Mon, 19-Oct-87 21:08:25 EDT Article-I.D.: PT.206 Posted: Mon Oct 19 21:08:25 1987 Date-Received: Wed, 21-Oct-87 00:18:36 EDT References: <507@umbc3.UMD.EDU> Sender: netnews@PT.CS.CMU.EDU Organization: Carnegie-Mellon University, CS/RI Lines: 53 In article <507@umbc3.UMD.EDU>, dipto@umbc3.UMD.EDU (Dipto Chakravarty ) writes: > > I have a prog that takes two parameters. The first one is a port and > the second is an integer that specifies the length of the string to > be sent to this port. > > If N=4 then I have to send a "\0\0\0\0" to the port. How can I write > a function that will take N as a parameter and return a string _out > (which consists of N "\0"s to my main(). char *build_null_string(N) int N; { char *temp; extern char *malloc(); temp = malloc(sizeof(char)*N); /* I actually never include the sizeof(char) */ bzero(temp,sizeof(char)*N); return(temp); } > > And secondly what form of fprintf or fwrite do I use to print out the > string from the main. The function itself can handle the output of the > string if passing it as parameter is messsy. > What you probably want something like this : #define WRITEBLOCK 4 write_null_chars(N,port) register int N; FILE *port; { register int i, j; static char a[WRITEBLOCK]; /* The static array should contain only zeros */ for (i = N; i >= WRITEBLOCK; i -= WRITEBLOCK) fwrite(a,sizeof(char),WRITEBLOCK,port); for (j = i; j > 0; j--) fwrite(a,sizeof(char),1,port); } -- Eddie Wyatt e-mail: edw@ius1.cs.cmu.edu