Re: c equivilant to "copy"
From: pete (pfiland_at_mindspring.com)
Date: 01/13/05
- Next message: Eric Sosman: "Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Previous message: Krishanu Debnath: "Re: c equivilant to "copy""
- In reply to: Krishanu Debnath: "Re: c equivilant to "copy""
- Next in thread: Stan Milam: "Re: c equivilant to "copy""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 13 Jan 2005 13:01:13 GMT
Krishanu Debnath wrote:
>
> Shagy wrote:
>
> >Greetings,
> >I've been trying to find an equivant c funtion to
> >the c++ copy function.
> >
> >Description:
> >
> > copy(char *cstring, size_t count, size_t offset);
> >
> > Copies "count" characters from a C-style string starting at offset.
> >
> >
> >Let's say I have the string "Hello There"
> >and I needed only the "llo" part from hello.
> > How would I accomplish this?
> >Using the c++ I could use the copy function.
> Probably what you are asking ...
>
> strncpy(dest, cstring + offset, count);
> dest[count] = '\0';
>
> assuming dest in large enough to hold 'count + 1' character.
Since you are not copying an entire string,
memcpy would work just as well as strncpy.
/* BEGIN new.c */
#include <string.h>
#include <stdio.h>
int main(void)
{
char dest[sizeof "llo"];
size_t count;
count = sizeof dest - 1;
memcpy(dest, "Hello There" + 2, count);
dest[count] = '\0';
puts(dest);
return 0;
}
/* END new.c */
- Next message: Eric Sosman: "Re: Implemenation Indepdent Way to Move LSByte of Char to MSB of Int, etc"
- Previous message: Krishanu Debnath: "Re: c equivilant to "copy""
- In reply to: Krishanu Debnath: "Re: c equivilant to "copy""
- Next in thread: Stan Milam: "Re: c equivilant to "copy""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|