On 20 October 2005, Ankit Gupta wrote:

> Hi
> I read this mail by i dont know who (may be shraddam sir) which said that he 
> did
> not understand the need for the "strcpy"  to return the pointer to the first 
> element. 
> But there is a problem of "counting the no of words"  where it is neccessary 
> for
> the pointer to return to the base else it seems impossible to make such a 
> program

Well consider the following snippet of code:

char * p, * q;
char buff[100];
/* Give q some value, then do: */
p = strcpy(buff, q);

Now in this code, the pointer p will _always_ point to the buffer. And we 
already know where buffer points to, as otherwise the code will not run. The 
value returned by strcpy is always its first argument, so the above code could 
equally well be:

char * p, * q;
char buff[100];
p = buff;
/* Give q some value, then do: */
strcpy(buff, q);

The two pieces of code have exactly the same effect. The only way you might not 
know the pointer to the first element copied would be in code like:

char * p = strcpy("apples", "bananas");

This may, or may not, change the string "bananas" to "apples", depending on 
whether it is in RAM or ROM, but in any case  the pointer p will point to the 
string. But again, you could have written the code as:

char * p = "apples";
strcpy(p, "bananas");

So, as I see it, the only useful value that strcpy could have been defined to 
return would be a pointer to the end of the string copied, or else a count of 
the number of characters copied. Or it should have been defined as void, and 
not return a value at all.

Anand Shraddhan

(There is a deliberate bug in the above code...)


[Non-text portions of this message have been removed]



------------------------ Yahoo! Groups Sponsor --------------------~--> 
Most low income households are not online. Help bridge the digital divide today!
http://us.click.yahoo.com/cd_AJB/QnQLAA/TtwFAA/EbFolB/TM
--------------------------------------------------------------------~-> 

>-----------------------------------------~-~>
CHECK THE ARCHIVE BEFORE POSTING!!!! Archive is available at 
http://www.eScribe.com/software/C-Paradise/

>------------------------------------------_->


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/C-Paradise/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to