It's UWAweek 48


This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

Displaying the 2 articles in this topic
Showing 2 of 919 articles.
Currently 2 other people reading this forum.

 UWA week 36 (2nd semester, mid-semester break) ↓
SVG not supported

Login to reply

1:03pm Tue 6th Sep, Haoxuan L.

char *strcpy_pointer( char *dest, char *src ) // two pointer parameters { char *origdest = dest; // take a copy of the dest parameter while( *src != '\0' ) { *dest = *src; // copy one character from src to dest ++src; ++dest; } *dest = '\0'; return origdest; // returns copy of original destination parameter } why do we need a *origdest variable? Can't we just return *dest?

SVG not supported

Login to reply

7:20am Wed 7th Sep, Christopher M.

"Haoxuan Long" <23*4*1*[email protected]*u*e*t*u*a*e*u*a*> wrote:
> why do we need a *origdest variable? Can't we just return *dest?
This example has followed the way the standard strcpy() function works. While C doesn't have a basic string datatype, we can't copy strings with string2 = string1; So strcpy() is written so that we can read it the same way - strcpy(string2, string1); Then, the result of the function is (the start of) string2, so that we can write: printf("result is %s\n", strcpy(string2, string1) ); or strcpy(string3, strcpy(string2, string1) ); analogous to: string3 = string2 = string1; There would be little point returning the value of dest in the provided implementation, as it's a pointer which always points to a null-byte, and we have no way of knowing which string that actually relates to.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  1:17AM Sep 14 2022
Privacy policy