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...
Printable View
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...
One way -
Code:void charcpy(char *dest,char *src,int where){
*dest=*(src + (where -1) );
*(dest+1)=0x00;
return;
}
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...
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.
BTW
another implementation could be
strncpy is part of the standard C runtime library, in the header <string.h>Code:char * charcpy(char *dest, const char *src, int where) {
return strncpy(dest, src+(where-1), 1);
}
Ok, thanks.
I will play around with it a bit and make sure I understand it
Or more verbose, requiring more instructions-
Actually under HPUX C *(dest+n) is reolved the fastest, that's why I use it, because pointer agruments are register variables by default.Code:void charcpy(char *dest, char *src, int where){
char *buf,*s;
char buf=dest;
where--;
s=src;
s+=where;
*buf++=*s;
*buf=0x00;
}
strncpy is just what I need, thank you!
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?
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.
Interesting...
But for
const char *
the register isn't const. The target of the pointer is.