473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

function explanation


What this function does?

void test_function(char *s, char *t) {

while(*s++=*t++)

;

}
--
Posted via http://dbforums.com
Nov 13 '05 #1
23 3470

"dominant" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

What this function does?

void test_function(char *s, char *t) {

while(*s++=*t++)

;

}
--
Posted via http://dbforums.com

Nov 13 '05 #2

"dominant" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

What this function does?

void test_function(char *s, char *t) {

while(*s++=*t++)

;

}


strcpy()

-Mike
Nov 13 '05 #3

"dominant" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

What this function does?

void test_function(char *s, char *t) {

while(*s++=*t++);


Remember that the = operator returns the value that
was assigned. Also remember that thew while loop
terminates when the controlling expression returns
0.

Then consider strings in C. They consist of a bunch of
characters followed by a null character. The null
character has value 0.

(Note that exactly when s and t are incremented is not
a straightforward issue for many beginners so this is
slightly informal, but is probably good enough for you)

First of s and t are incremented, ie. they are made to
point to the next char in the string they are pointing
to. Then their old value is returned for the * operator
to use. This means that value t was (since this is after
the increment) pointing to is now assigned to the position
s was pointing to. This value is then inspected by the while
loop to see if this value is 0 or not. If it is non-zero then
the loop goes again, but this time s and t are pointing
to different positions. So when t is pointing to a null
terminator, this value (aka 0) is assigned to *s and the while
loop gets a 0 and terminates.

The while loop can also be written like this:

while(*s = *t)
{
t++;
s++;
}

Note that the version you posted makes t potentially point
outside the array it is pointing to[1]. This is however legal
as the standard allows this to cater for the above idiom and
friends. You are not allowed to dereference this pointer even
if it is pointing somewhere legal.

[1] It is actually pointing to elements in the array, but you get
my drift.

--
Thomas.
Nov 13 '05 #4

"dominant" <me*********@dbforums.com> wrote in message

void test_function(char *s, char *t)
while(*s++=*t++)
;
}

This is terrible C style. The equals sign is not a equality comparison, as
it looks like give the context, but an assignment. When the result of the
assignment, *s, goes to zero, the loop will terminate. The semicolon
indicates that it is an empty loop.
The ++ operators increment the pointers, and are applied after the
assignment takes place. This you wouldn't know if you didn't know C, but is
idiomatic and therefore just acceptable.
The combination of three quirky C features in one short loop makes this code
very difficult to read and debug.


Nov 13 '05 #5

"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:bk**********@newsg3.svr.pol.co.uk...

"dominant" <me*********@dbforums.com> wrote in message

void test_function(char *s, char *t)
while(*s++=*t++)
;
} The combination of three quirky C features in one short loop makes this

code very difficult to read and debug.


Given the fact that it is a well known and accepted idiom
it is neither hard to read or hard to debug. First time I saw
it it took me about 1 minute to figure out what this was
doing.

--
Thomas.
Nov 13 '05 #6
Thomas Stegen wrote:

"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:bk**********@newsg3.svr.pol.co.uk...

"dominant" <me*********@dbforums.com> wrote in message
[snip]
(char *s, char *t)
while(*s++=*t++)
;
}
The combination of three quirky C features
in one short loop makes this code
very difficult to read and debug.

That code is taken directly from page 106, K&R2, section 5.5.
Given the fact that it is a well known and accepted idiom
it is neither hard to read or hard to debug.


"... the idiom should be mastered,
because you will see it frequently in C programs."

--
pete
Nov 13 '05 #7
"pete" <pf*****@mindspring.com> wrote in message
news:3F***********@mindspring.com...
....
> (char *s, char *t)
> while(*s++=*t++)
> ;


"... the idiom should be mastered,
because you will see it frequently in C programs."


I can't speak for anyone else, but it was the extraordinary succinctness of
this paradigm that drew me to C in the first place.

Although, I've since been told that for many architectures, the following is
more likely to generate efficient machine code...

if (*s = *t)
while (*++s = *++t)
;

I guess learning 68000 assembler before I saw C was both an advantage and
disadvantage.

--
Peter
Nov 13 '05 #8

"Peter Nilsson" <ai***@acay.com.au> wrote in message
> > (char *s, char *t)
> > while(*s++=*t++)
> > ;

I can't speak for anyone else, but it was the extraordinary succinctness >

of this paradigm that drew me to C in the first place.

It shouldn't do. Programs are designed to present a series of instructions
to a computer, but they must also present code to another human programmer.
Generally the cost of a programmmer's time reading and understanding code
far exceeds the cost of computer time executing it. There are exceptions,
such as the inner loops of time-critical programs like games, but as a rule
code should be written so that it is as easy as possible to understand.
You can assume that your reader knows the C basics, and knows how to
program, but you shouldn't necessarily assume that he is comfortable with C
arcana. In particular constructions such as
while(a = b)
which suggest the wrong meaning should be avoided.

Nov 13 '05 #9
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message news:<bk**********@news8.svr.pol.co.uk>...
"Peter Nilsson" <ai***@acay.com.au> wrote in message
> > > (char *s, char *t)
> > > while(*s++=*t++)
> > > ;
I can't speak for anyone else, but it was the extraordinary succinctness
of this paradigm that drew me to C in the first place.


It shouldn't do. Programs are designed to present a series of instructions
to a computer, but they must also present code to another human programmer.
Generally the cost of a programmmer's time reading and understanding code
far exceeds the cost of computer time executing it.


The 'understanding' you speak of is an aquired skill and not something
easily quantifiable. Languages like Lisp and Forth are quite often far
from obvious to the lay programmer, but they both offer aesthetic
clarity and elegance in constructs that aren't easily understood by
people for whom the languages haven't 'clicked' yet.

Any C programmer who has difficulty understanding the above
construction should not be let anywhere near anything even remotely
critical. Programs should be written with clarity in mind, but the
whole point of language development is to encapsulate paradigms with
clean syntax.

I've seen people use constructs like...

while (...yadda...)
{
continue;
}

It's my personal opinion obviously, but arguments stating that the
presence of the last three lines in place of a semi-colon aids
readability are an insult to the intelligence of moderately competent
C programmers.
There are exceptions,
such as the inner loops of time-critical programs like games, but as a rule
code should be written so that it is as easy as possible to understand.
What is difficult to understand about...?

while (*s++ = *t++)
;
You can assume that your reader knows the C basics,
No. If you're talking about non toy programs, you can assume that the
reader knows more than the basics. If you don't then you must be
anticipating your code being modified and subsequently distributed by
people not really qualified to so.
and knows how to
program, but you shouldn't necessarily assume that he is comfortable with C
arcana. In particular constructions such as
while(a = b)
which suggest the wrong meaning should be avoided.


I would say 'In contrast...' The meaning of original construct is IMHO
self evident.

--
Peter
Nov 13 '05 #10
On 25 Sep 2003, Peter Nilsson wrote:
What is difficult to understand about...?

while (*s++ = *t++)
;


First, there's an off by one error. Second, it doesn't terminate the
destination string.

;)
Nov 13 '05 #11
"Thomas Stegen" <ts*****@cis.strath.ac.uk> wrote in message news:<3f********@nntphost.cis.strath.ac.uk>...
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:bk**********@newsg3.svr.pol.co.uk...

"dominant" <me*********@dbforums.com> wrote in message

void test_function(char *s, char *t)
while(*s++=*t++)
;
}

The combination of three quirky C features in one short loop makes this

code
very difficult to read and debug.


Given the fact that it is a well known and accepted idiom
it is neither hard to read or hard to debug. First time I saw
it it took me about 1 minute to figure out what this was
doing.


also, the pattern of "while (*src_ptr++ = *dst_ptr++)" is
fairly common, and I personally find it intuitive these days
when looking at code. it tells you (or rather, it /should/) the
intent of the programmer without needing a single comment.

goose,
Nov 13 '05 #12
Jarno A Wuolijoki wrote:
On 25 Sep 2003, Peter Nilsson wrote:
What is difficult to understand about...?

while (*s++ = *t++)
;


First, there's an off by one error. Second, it doesn't terminate
the destination string.


No there isn't (an error). Yes it does (terminate). All assuming
the source is a legitimate string and source and destination do
not overlap.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #13
CBFalconer <cb********@yahoo.com> spoke thus:
> while (*s++ = *t++)
> ;
No there isn't (an error). Yes it does (terminate). All assuming
the source is a legitimate string and source and destination do
not overlap.


And also that s has space for strlen(t)+1 characters.

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #14
Jarno A Wuolijoki <jw******@cs.Helsinki.FI> wrote in message news:<Pi**************************************@mel kinpaasi.cs.Helsinki.FI>...
On 25 Sep 2003, Peter Nilsson wrote:
What is difficult to understand about...?

while (*s++ = *t++)
;
First, there's an off by one error.


Where?
Second, it doesn't terminate the destination string.

;)


Only if the source string isn't terminated, either.
Nov 13 '05 #15
On Fri, 26 Sep 2003, CBFalconer wrote:
What is difficult to understand about...?

while (*s++ = *t++)
;


First, there's an off by one error. Second, it doesn't terminate
the destination string.


No there isn't (an error). Yes it does (terminate).


And that's supposed to be easy to understand?
*t++; /* "get()" */
*s++=x; /* "put(x)" */
*s++=*t++; /* "put(get())" */

while (tmp=*t++) *s++=tmp;
/* "as long as we can get data, append it to s" */
*s=0;
/* terminate s; */

while (*t) *s++=*t++;
*s=0;
/* "as long as there's data, copy it." */

while (*s++=*t++) ;
/* "as long as we have copied nonzero data, do nothing. perform extra
copy as a side effect of embedding it in the test" */

(of course if you interpret "*s++=*t++" as a routine which returns 0
when it has finished it makes a whole lot more sense..)

Nov 13 '05 #16
Jarno A Wuolijoki <jw******@cs.Helsinki.FI> wrote:
On Fri, 26 Sep 2003, CBFalconer wrote:
> > What is difficult to understand about...?
> >
> > while (*s++ = *t++)
> > ;
>
> First, there's an off by one error. Second, it doesn't terminate
> the destination string.


No there isn't (an error). Yes it does (terminate).


And that's supposed to be easy to understand?


Yes. :)

<SNIP>

Irrwahn
--
ERROR 103: Dead mouse in hard drive.
Nov 13 '05 #17
CBFalconer wrote:

Jarno A Wuolijoki wrote:
On 25 Sep 2003, Peter Nilsson wrote:
What is difficult to understand about...?

while (*s++ = *t++)
;


First, there's an off by one error. Second, it doesn't terminate
the destination string.


No there isn't (an error). Yes it does (terminate). All assuming
the source is a legitimate string and source and destination do
not overlap.


It depends on how they overlap.

--
pete
Nov 13 '05 #18

"Jarno A Wuolijoki" <jw******@cs.Helsinki.FI> wrote in message
On Fri, 26 Sep 2003, CBFalconer wrote:
> What is difficult to understand about...?
>
> while (*s++ = *t++)
> ;

First, there's an off by one error. Second, it doesn't terminate
the destination string.


No there isn't (an error). Yes it does (terminate).


And that's supposed to be easy to understand?

Exactly. We have a construct that is supposedly "easy to understand",
perfectly acceptable, idiomatic C, and first a newbie posts with "what does
this do" and then another poster mistakes it for a bug.

Most of C is basically the same as any other programming language. Someone
who knows another language will easily understand

int x = 0;

he will also quickly get

for(i=0;i<100;i++)

even though ++ is a pure C idiom.

When it comes to pointers, he has some learning to do. He can quickly pick
up that * is the indirection operator. A bit more difficult is that
*ptr++ increments ptr, rather than *ptr, despite the fact that the ++ is
applied last.

However when we come to

while(*s++ = *t++);

it is a bridge too far for our experienced programmer who only knows a bit
of C. Unless you know, it is deeply counter intutive that = in this context
means "assign" rather than "compare". If he's lucky he will remember the
pointer incrementing and dereference rules, but only just, and this will
make him uncomfortable and unfamiliar with the whole expression. Finally,
using a semi-colon to denote an empty loop is also something that has few
parallels in other languages, and looks like a syntax error.

In short, this expression will make the code harder to read and is likely to
cause trouble. Even an experienced C programmer, though he will not
misinterpret it, will probably take longer to read and understand the
expression and verify that it is correct. This is particularly the case if
it is embedded in a list of other expressions which are also
counter-intutive and difficult to understand.
Nov 13 '05 #19
Malcolm wrote:
Even an experienced C programmer, though he will not
misinterpret it, will probably take longer to read and understand the
expression and verify that it is correct.


"... the idiom should be mastered,
because you will see it frequently in C programs."

Mastering the idiom means, that you will recognize it,
and know at a glance, what it means.

If you have not mastered it, then it may be tough.
But the mastery that particular idiom, is more common than not,
among experienced C programmers.

A few snippets for you:

void copyarray(e_type *s1, e_type *s2, size_t nmemb)
{
while (nmemb--) {
*s1++ = *s2++;
}
}

char *squeeze(char *s1, const char *s2)
{
char const* p2;
char *const p1 = s1;

for (p2 = strtok(s1, s2); p2; p2 = strtok( 0, s2)) {
while (*p2) {
*s1++ = *p2++;
}
}

*s1 = '\0';
return p1;
}

static void merge(e_type *base, e_type *buffer, size_t nmemb)
{
if (nmemb > SMALL_MERGE) {
size_t const half = nmemb / 2;
e_type *const middle = base + half;
e_type *const after_buffer = buffer + half;
e_type *const after_array = base + nmemb;
e_type *mid_ptr = middle;

merge(base, buffer, half);
merge(middle, buffer, nmemb - half);
while (GTE(middle, base) && middle != base) {
++base;
}
buffer = after_buffer;
while (base != mid_ptr) {
*--buffer = *--mid_ptr;
}
mid_ptr = middle;
while (middle != base) {
*base++ = GT(buffer, mid_ptr) ? *mid_ptr++ : *buffer++;
}
while (after_buffer != buffer && after_array != mid_ptr) {
*base++ = GT(buffer, mid_ptr) ? *mid_ptr++ : *buffer++;
}
while (after_buffer != buffer) {
*base++ = *buffer++;
}
} else {
si_sort(base, nmemb);
}
}

--
pete
Nov 13 '05 #20
Jarno A Wuolijoki <jw******@cs.Helsinki.FI> wrote in message news:<Pi*************************************@melk inpaasi.cs.Helsinki.FI>...
On Fri, 26 Sep 2003, CBFalconer wrote:
> What is difficult to understand about...?
>
> while (*s++ = *t++)
> ;

First, there's an off by one error. Second, it doesn't terminate
the destination string.


No there isn't (an error). Yes it does (terminate).


And that's supposed to be easy to understand?


Yes, after your brain has been sufficiently damaged^H^H^H^H^H^H^H
trained to recognize certain C idioms.
Nov 13 '05 #21
On Mon, 29 Sep 2003, pete wrote:
Even an experienced C programmer, though he will not
misinterpret it, will probably take longer to read and understand the
expression and verify that it is correct.


"... the idiom should be mastered,
because you will see it frequently in C programs."

Mastering the idiom means, that you will recognize it,
and know at a glance, what it means.

If you have not mastered it, then it may be tough.
But the mastery that particular idiom, is more common than not,
among experienced C programmers.

A few snippets for you:


Note that the "idiomatic strcpy" performed a transgression that
none of your perfectly readable snippets did:

while (loop_body) /*nothing*/ ;

Sure I know what it really does, but then again, I know !strcmp(a, b)
as well.

Nov 13 '05 #22
Jarno A Wuolijoki <jw******@cs.helsinki.fi> wrote:
On Mon, 29 Sep 2003, pete wrote:
> Even an experienced C programmer, though he will not
> misinterpret it, will probably take longer to read and understand the
> expression and verify that it is correct.


"... the idiom should be mastered,
because you will see it frequently in C programs."

Mastering the idiom means, that you will recognize it,
and know at a glance, what it means.

If you have not mastered it, then it may be tough.
But the mastery that particular idiom, is more common than not,
among experienced C programmers.

A few snippets for you:


Note that the "idiomatic strcpy" performed a transgression that
none of your perfectly readable snippets did:

while (loop_body) /*nothing*/ ;


while (sideeffects) { }

might well be a clearer way of writing it. It's pretty hard to
misinterpret that.

- Kevin.

Nov 13 '05 #23
Kevin Easton wrote:
.... snip ...
while (sideeffects) { }

might well be a clearer way of writing it. It's pretty hard to
misinterpret that.


Which can be used to evolve a fairly efficient strlen:

while (*s++) continue;

Now all we have to know is how many time s was advanced, leading
to:

size_t strlen(char *s)
{
char *p = s;

while (*s++) continue;
return s - p - 1;
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
8
by: James Fortune | last post by:
I'm doing some computations in order to do capacity planning. Instead of using some function from an Access book to do the weekday calculation, I decided to come up with an alternate method since...
1
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
9
by: ypjofficial | last post by:
Hello All, I am defining a class with one virtual function and storing its first 4 bytes ie. the address of the virtual function table to a file.I am again rereading the file in the same program...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
1
by: Alan | last post by:
I now know how to compose a function object for some standard algorithms, e.g., std::sort(). However, I cannot find an explanation of how to use a function object in my own code of an algorithm. ...
15
by: Sampat | last post by:
Hi, I wanted to know the performance of calling a function pointer v/s a normal function call in javascript in a scenario where there are multiple calls in the js to the same function. Please...
18
by: Ron Croonenberg | last post by:
Hello, I am somewhat new to Javascript (except for the occasional short script I never used it that that much... Anyway, I am writing a script and have a function that need to change 2...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.