String Reversal (kinda different from the rest)

This is a discussion on String Reversal (kinda different from the rest) within the C Programming forums, part of the General Programming Boards category; I got a comment about my code that it was C99. Just for my own understanding, can someone clear up ...

  1. #16
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    I got a comment about my code that it was C99. Just for my own understanding, can someone clear up for me what's C99 about it? Is it the way I declared strarray? I really should get my hands on some standards books

    Thanks
    If you understand what you're doing, you're not learning anything.

  2. #17
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Here's another one. It uses the XOR trick, but you can use another temporary variable to swap characters.

    Code:
    char *revstr(char *const string)
    {
    		register char   *begin;
    		register char   *end;
    		const size_t	 length = strlen(string);
    
    
    		for (begin = string, end = string + length - 1; begin < end; begin++, end--)
    				*begin ^= *end ^= *begin ^= *end;
    
    		return string;
    }
    PS: itsme86: Yes, it's the way you declared strarray (array size computed in run-time).
    "In My Egotistical Opinion, most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know there's really no point in ever using the 'register' keyword, right?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > *begin ^= *end ^= *begin ^= *end;
    Oh please, not this nonsense again!!!!

    http://www.eskimo.com/~scs/C-faq/q10.3.html
    Pay REALLY close attention now - read the bit which says

    "and the ``obvious'' supercompressed implementation for integral types a^=b^=a^=b is illegal due to multiple side-effects; see question 3.2)."

    I seem to remember a post of mine some time ago which showed that the compiler was way smarter than the "clever" programmer in that the blindingly obvious "use a temporary variable" actually produced the smallest code, and that all these arithmetic and logical tricks produced longer instruction sequences.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by itsme86
    I got a comment about my code that it was C99. Just for my own understanding, can someone clear up for me what's C99 about it? Is it the way I declared strarray? I really should get my hands on some standards books

    Thanks

    Variable-size arrays in C have always been (pre-C99) a no-no:
    Code:
    void revstr(char *str)
    {
    
      struct str_s
      {
        char ch;
        char *ptr;
      } strarray[strlen(str)], temp;
    (You do realize, don't you, that there's a little elf ---near the East Pole--- who collects a royalty of .01 euros for every use of malloc() in a commercial program? If everybody started using variable-length arrays, he would have to declare bankruptcy. The next thing you know, he would given his own reality-TV show. Too horrible to imagine!)

    Regards,

    Dave
    Last edited by Dave Evans; 02-17-2005 at 12:37 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21