🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.atari.st
Article #39501

Re: more on strncpy (was re: laser c question)

#39501
From: kirkenda Steve Kirkendall
Date: Fri, 15 Dec 1989 20:41
78 lines
3675 bytes
(I would have mailed this, but none of the machines around here have ever heard
 of Canada before.  Or any other country either.)

In article <1870@calvin.cs.mcgill.ca> depeche@calvin.cs.mcgill.ca (Sam Alan EZUST) writes:
>someone already sent me dlibs memcpy which I believe is what is used
>in Sozobon.... However, I don't have an assembler and am not too
>much of an expert in it anyway, so I couldn't get it installed on
>my system without help.

Okay, here's one in C:

	memcpy(dest, src, count)
		register char	*dest;	/* destination address */
		register char	*src;	/* source address */
		register int	count;	/* number of bytes to copy */
	{
		while (--count >= 0)
			*dest++ = *src++;
	}

This can copy up to 32767 bytes at a time.  It isn't very fast, though.

>:   Alternately, does Lazer C allow copying structs using a simple
>: assignment?  <struct2> = <struct1>;  If so, what I've ocassionaly used in
>: situations like yours is to define a struct containing the array and
>: moved copys of the array arround by just using assignments...
>
>that's a good idea. I should try it. I didn't think it was possible though,
>as struct names are just pointers to memory locations of data, just
>like arrays, and you can address the 5th byte of a struct with
>*(structname+5) just like arrays.

No.  A struct variable is not treated as a pointer.  If you do something
like this...

	struct dirent	d;
	somefunc(d);

... then somefunc will be called with a copy of d as its argument,
and the copy will be sizeof(struct dirent) bytes long, on the stack!
It does *not* pass a pointer to d.  Laser C can handle struct assignments.

And any attempt to add an int to a struct should net you an error message.

> in reply to my using strncpy for multidimension arrays not working,
>
>:   Why won't it?  Ignoring for the moment that strncpy isn't a blkcpy or
>: memcpy, what's wrong with specifiying &array[0][0] as the address and
>: using the size of the entire array (assuming it's smaller than 64k bytes)
>: as the length?
>
>I think I tried it and got a compiler error.. I could be mistaken, but
>I passed it just 
>
>array
>
>instead of
>
>&(array[0][0])
>since I thought the two were equivalent pointers...

They both point to the same place in memory, but they are pointers to different
types of data.  The first form is a pointer to an array (a vector, actually --
the first column of your two-dimensional array).  The second is a pointer to a
single element.  The distinction isn't very important when you're using the
expression as an argument to memcpy(), though.

The simplest way to copy a two-dimensional array with memcpy() is:

	memcpy(destarray, srcarray, sizeof srcarray);

Some compilers may complain about this since the first two arguments are not
of type (char *).  You may wish to coerce them to get rid of the warning and
improve portability, at the expense of readability...

	memcpy((char *)destarray, (char *)srcarray, sizeof srcarray);
--------------------------------------------------------------------------------
Steve Kirkendall, kirkenda@cs.pdx.edu, uunet!tektronix!psueea!jove!kirkenda
Technical Details
Message-ID: <2095@psueea.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!zephyr.ens.tek.com!tektronix!psueea!!kirkenda
References: <1870@calvin.cs.mcgill.ca>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!zephyr.ens.tek.com!tektronix!psueea!!kirkenda
From: kirkenda@.cs.pdx.edu (Steve Kirkendall)
Newsgroups: comp.sys.atari.st
Subject: Re: more on strncpy (was re: laser c question)
Message-ID: <2095@psueea.UUCP>
Date: 15 Dec 89 20:41:54 GMT
References: <1870@calvin.cs.mcgill.ca>
Sender: news@psueea.UUCP
Reply-To: kirkenda@cs.pdx.edu (Steve Kirkendall)
Organization: /etc/organization
Lines: 78