Results 1 to 11 of 11

Thread: strcpy?

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    strcpy?

    How do I copy the second character in a string to another string?
    I've tried strcpy(strTemp, &(str[1]); but it didn't work...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    One way -

    Code:
    void charcpy(char *dest,char *src,int where){
            *dest=*(src + (where -1) );
            *(dest+1)=0x00;
            return;
    }

  3. #3

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Thank you so much!
    Could you please explain how the code works? I'm not very good at this string manipulating stuff... I do know how to use pointers but not very well...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Why the -1 jim? Isn't it confusing if your function uses a 1-based index?
    And for the sake of good style the source should be const char*.
    The code is quite simple.
    The arguments are:
    dest: the destination. A char buffer at least 2 chars long.
    src: the source string.
    where: the 1-based index into src of the character to copy.

    *dest=*(src + (where -1) );
    *dest
    dest is dereferenced. This means the assign operation assigns to the character dest points at, not the pointer itself. It is equivalent to
    dest[0]

    *(src + (where -1) )
    This is equivalent to
    arc[where-1]
    The -1 is because where is 1-based but arrays in C are 0-based.
    So this takes the whereth character of src and copies it to the first position in dest.

    *(dest+1)=0x00;
    This adds a terminating NUL-character to dest in order to make it a valid C-string.
    *(dest+1)
    is equivalent to
    dest[1]
    so it accesses the second position of the buffer (the first is where the copied character is).

    return;
    This line is redundant in ANSI C but probably required by some of the weird compilers jim has or had to work with.
    Last edited by CornedBee; Dec 23rd, 2002 at 07:01 PM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    BTW

    another implementation could be
    Code:
    char * charcpy(char *dest, const char *src, int where) {
      return strncpy(dest, src+(where-1), 1);
    }
    strncpy is part of the standard C runtime library, in the header <string.h>
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, thanks.
    I will play around with it a bit and make sure I understand it
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  7. #7
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Or more verbose, requiring more instructions-
    Code:
    void charcpy(char *dest, char *src, int where){
                 char *buf,*s;
                 char buf=dest;
                 where--;
                 s=src;
                 s+=where;
                 *buf++=*s;
                 *buf=0x00;
    }
    Actually under HPUX C *(dest+n) is reolved the fastest, that's why I use it, because pointer agruments are register variables by default.

  8. #8

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    strncpy is just what I need, thank you!
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    So like I guessed, stupid compilers. I've heard bad things about the HPUX C++ compiler, especially at the Mozilla cross-compiler guidances page.

    But still, why do you make where 1-based and why don't you make src const?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    register variables can't be const in HPUX C- that's why for this example.

    What's going on - my job sometimes involves re-writing code sections, even into asm, when it doesn't run fast. Or it isn't readable. Not that the above stuff was readable.

    strncpy implementation on HPUX, as you see it in the .as file, is pretty much what I wrote, translated from asm.

    I've written MSVC++, Borland, gcc code, and lots of others. I hate not answering a question - but end up being pedantic too often. You can't tell what people know on this Forum.

    By the way, strncpy in MSVC++, in my opinion, is not a great choice. I had to work around it about 6 months ago.
    For 240 million records with about 20 strncpy calls per record, I sped the loop up by a factor of 230%.

    Speaking of weird compilers:

    FWIW - if you are into performance and are okay with SDK windows, look into PowerBasic. I know. BASIC and speed are non-sequitor.

    PB string handling blows the socks off the standard string library in MSVC++. I write dll's with it all the time. The fix mentioned above is using PB - it also supports inline asm and pointers. In other words it's almost like C with BASIC syntax.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Interesting...

    But for
    const char *
    the register isn't const. The target of the pointer is.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width