(no subject) 
Author Message
 (no subject)

Hi folks,

In the following code, what does memmove do?  What is the difference
between ++i and i++?  Thanks for any help you can provide.

i = 1;
        memmove(REK[0], "None ", 5);
        while(fgets(iline, 70, d_file) != NULL)
        {
                if(i == 1)
                {
                        memmove(REK[i], iline, 5);
                        ++i;
                }
                else
                {
                        if(strncmp(REK[i - 1], iline, 5))
                        {
                                memmove(REK[i], iline, 5);
                                ++i;
                        }
                }
        }



Fri, 01 Jan 1999 03:00:00 GMT  
 (no subject)

Quote:
>In the following code, what does memmove do?  What is the difference
>between ++i and i++?  Thanks for any help you can provide.

No idea.  You haven't given us the declaration for REK, so....  We don't know if REK
is a pointer to a pointer, or if it's a pointer.

i.e.  Is REK defined as follows:  char *REK[]?  Or is it defined as follows:  
char REK[]?

BTW, it might help if you could elaborate on what you are trying to accomplish
with the following code.

The difference between i++ and ++i, is that one method uses a post-increment,
while the other uses a pre-increment.  i.e.  the former will process the current
line before increasing the value of i.  The latter will process the increment, then
process the line.  However, even when it processes the increment first, it must
still follow C's order of operations.

+-------------------------------------------------------------+
: The opinions in this message do not necessarily reflect     :
: those of my employer.                                       :
+-------------------------------------------------------------+

: Author of Enitharmon/2 BBS and various OS/2 utilities.      :
: New Westminster, BC, Canada                                 :
+-------------------------------------------------------------+



Fri, 01 Jan 1999 03:00:00 GMT  
 (no subject)


Quote:
>The difference between i++ and ++i, is that one method uses a
>post-increment, while the other uses a pre-increment.  i.e.  the
>former will process the current line before increasing the value of i.
>The latter will process the increment, then process the line.  

This is not correct! [Moderator, where were you?  Again!]

With post-increment, i++, the value of i is evaluated *in the
expression in which it appears* before the value of i is incremented.  
With pre-increment, ++i, the value of i is incremented before its value
is evaluated in the expression.

The distinction between this definition and the one posted is very
important.  'C' is free to compile code which evaluates the terms of an
expression between sequence points in any order, irrespective of the
precedence of the operators in the expression.  It is only the
evaluation of the operations themselves that must be done in precedence
and parenthesised order.

So, in post increment, the value is not incremented after evaluating
the statement, but at some indeterminate time during the evaluation of
the statement.

It is this fact that makes the value of expressions like (i++ + a[i])
undefined.

[mailed & posted]
--
Ray Dunn (opinions are my own) | Phone: (514) 938 9050
Montreal                       | Phax : (514) 938 5225



Sat, 02 Jan 1999 03:00:00 GMT  
 (no subject)

[Posted as well as mailed]

Quote:

> Hi folks,

> In the following code, what does memmove do?  What is the difference
> between ++i and i++?  Thanks for any help you can provide.

> i = 1;
>         memmove(REK[0], "None ", 5);
>         while(fgets(iline, 70, d_file) != NULL)
>         {
>                 if(i == 1)
>                 {
>                         memmove(REK[i], iline, 5);
>                         ++i;
>                 }
>                 else
>                 {
>                         if(strncmp(REK[i - 1], iline, 5))
>                         {
>                                 memmove(REK[i], iline, 5);
>                                 ++i;
>                         }
>                 }
>         }

(The question about ++i and i++ already answered in an earlier post.)
In this case you can just think of the "memmove" as a fast strncpy.
Note there are some subtle differences (or not so subtle.)

Now here is a question for some one else:
In the K&R II book, about the strncpy function it says, I quote:
"Pad with '/0's if t has fewer than n  characters." given the following
prototype: char *strncpy( s, ct, n )
BUT my man page says:
"The copy operation stops when a null character is copied."

Also, about the code, if you know that the current line of the file
when you reach that point will not start with "None ", then the following
code will also work:

i = 1;
        memmove(REK[0], "None ", 5);
        while(fgets(iline, 70, d_file) != NULL)
             if(strncmp(REK[i - 1], iline, 5))
                  memmove(REK[i++], iline, 5);

--
Samuel O.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



Mon, 04 Jan 1999 03:00:00 GMT  
 (no subject)

Quote:

> Now here is a question for some one else:
> In the K&R II book, about the strncpy function it says, I quote:
> "Pad with '/0's if t has fewer than n  characters." given the following
> prototype: char *strncpy( s, ct, n )
> BUT my man page says:
> "The copy operation stops when a null character is copied."

Can't see that these are mutually exclusive. Example:

main()
{
    char ABuf [] = "abcde", BBuf[] = "abcde";
    strncpy(ABuf, "foo", 6);
    strncpy(BBuf, "foobar", 6);

Quote:
}

Now, ABuf will contain {'f','o','o',0,0,0}, and BBuf = {'f','o','o','b','a','r'}.
strncpy just fills up the rest of the buffer with zeroes; however, it doesn't always
produce a trailing zero. I think stccpy() does this.

--

Quote:
}\ /{ Troppsleder Ulven Speidergruppe/NSF     // Amiga  1200/2MB/120MB

{o~o} http://www.ifi.uio.no/~jensthi/scout/ \X/  PGP: 889FEE5F4481AE55
 (")  ICBM address: N59056.25'. E10043.37'            418685BD14C02604
  U   Phone: +47 22720333 BBS: +47 22733385    #include <disclaimer.h>


Tue, 05 Jan 1999 03:00:00 GMT  
 (no subject)


: > Now here is a question for some one else:
: > In the K&R II book, about the strncpy function it says, I quote:
: > "Pad with '/0's if t has fewer than n  characters." given the following
: > prototype: char *strncpy( s, ct, n )
: > BUT my man page says:
: > "The copy operation stops when a null character is copied."

: Can't see that these are mutually exclusive. Example:

: main()
: {
:     char ABuf [] = "abcde", BBuf[] = "abcde";
:     strncpy(ABuf, "foo", 6);
:     strncpy(BBuf, "foobar", 6);
: }

: Now, ABuf will contain {'f','o','o',0,0,0}, and BBuf = {'f','o','o','b','a','r'}.
: strncpy just fills up the rest of the buffer with zeroes; however, it doesn't always
: produce a trailing zero. I think stccpy() does this.

I think what Samuel meant was that say I have
char a[15]="alrighty then",b[15]="hello";

strncpy(a,b,10);
the loop in strncpy will go 10 times
and produce
{'h','e','l','l','o',0,0,0,0,0,'h','e','n',0,0}
not
{'h','e','l','l','o',0,'t','y',' ','t','h','e','n',0,0}
at least according to K&R2
the leftover is from the string that was there before. It is not changed.

---------------------------------------------------------------------


Canada
---------------------------------------------------------------------



Tue, 05 Jan 1999 03:00:00 GMT  
 (no subject)

 > Now here is a question for some one else:
 > In the K&R II book, about the strncpy function it says, I quote:
 > "Pad with '/0's if t has fewer than n  characters." given the following
 > prototype: char *strncpy( s, ct, n )
 > BUT my man page says:
 > "The copy operation stops when a null character is copied."

They're both right.  After the nul has been copied, the
copying stops and the padding begins.



Tue, 05 Jan 1999 03:00:00 GMT  
 (no subject)



[snip]

Quote:
> I think what Samuel meant was that say I have
> char a[15]="alrighty then",b[15]="hello";

> strncpy(a,b,10);
> the loop in strncpy will go 10 times
> and produce
> {'h','e','l','l','o',0,0,0,0,0,'h','e','n',0,0}
> not
> {'h','e','l','l','o',0,'t','y',' ','t','h','e','n',0,0}
> at least according to K&R2
> the leftover is from the string that was there before. It is not changed.

I quote from The C Programming Language, 2nd ed. 1988,
ISBN 0-13-110362-8 B.3 String Functions: <string.h> p.249

"char *strncpy(s,ct,n) copy at most n characters of string ct to s; return s.
                      Pad with '\0's is t has fewer than n characters."

That is the only reference to strncpy in the index.  Allowing for the
typo of referring to t when ct was meant; it seems pretty clear.

Does your 2nd ed. says something different?

Regardless, K&R2 is a secondary source; the behavior of strncpy is clear
if strange - I remain astonished it does not behave in the manner you
say is documented rather than as documented im ANSI/ISO 9899-1990.
--
Walter Briscoe



Fri, 08 Jan 1999 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Subject: Top 10 subjects in comp.lang.c {From: mikey@ontek.com (Mike Lee)}

2. Subject: Top 10 subjects in comp.lang.c {From Mike Lee, via me}

3. No subject was specified.

4. Do my homework, I'm lazy [submitted without subject -mod]

5. (no subject)

6. Newbie Question [came in with no subject line -mod]

7. matrix determinant code [submitted with no subject]

8. (no subject)

9. <sent to mod without subject>

10. (no subject)

11. Top 10 subjects in comp.lang.c

12. Top 10 subjects in comp.lang.c

 

 
Powered by phpBB® Forum Software