asyncwait
asyncwait

Reputation: 4537

Copying some strings from pointer array in C++

I have a string pointer like below,

char *str = "This is cool stuff";

Now, I've references to this string pointer like below,

char* start = str + 1;
char* end = str + 6;

So, start and end are pointing to different locations of *str. How can I copy the string chars falls between start and end into a new string pointer. Any existing C++/C function is preferable.

Upvotes: 0

Views: 1358

Answers (7)

Michael Burr
Michael Burr

Reputation: 340466

Assuming that end follows the idiomatic semantics of pointing just past the last item you want copied (STL semantics are a useful idiom even if we're dealing with straight C) and that your destination buffer is known to have enough space:

memcpy( buf, start, end-start);
buf[end-start] = '\0';

I'd wrap this in a sub-string function that also took the destination buffer size as a parameter so it could perform a check and truncate the result or return an error to prevent overruns.

I'd avoid using strncpy() because too many programmers forget about the fact that it might not terminate the destination string, so the second line might be mistakenly dropped at some point by someone believing it unnecessary. That's less likely if memcpy() were used. (In general, just say no to using strncpy())

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320747

This is one of the rare cases when function strncpy can be used. Just calculate the number of characters you need to copy and specify that exact amount in the strncpy. Remember that strncpy will not zero-terminate the result in this case, so you'll have to do it yourself (which, BTW, means that it makes more sense to use memcpy instead of the virtually useless strncpy).

And please, do yourself a favor, start using const char * pointers with string literals.

Upvotes: 0

Paul Tomblin
Paul Tomblin

Reputation: 182880

char newChar[] = new char[end-start+1]]
p = newChar;
while (start < end)
  *p++ = *start++;

Upvotes: 0

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

Use STL std::string:


#include 
const char *str = "This is cool stuff";
std::string part( str + 1, str + 6 );

This uses iterator range constructor, so the part of the C-string does not have to be zero-terminated.

Upvotes: 2

Alex Reynolds
Alex Reynolds

Reputation: 96984

If you want to do this with C++ STL:

#include <string>
...
std::string cppStr (str, 1, 6); // copy substring range from 1st to 6th character of *str
const char *newStr = cppStr.c_str(); // make new char* from substring

Upvotes: 0

unwind
unwind

Reputation: 400109

It's best to do this with strcpy(), and terminate the result yourself. The standard strncpy() function has very strange semantics.

If you really want a "new string pointer", and be a bit safe with regard to lengths and static buffers, you need to dynamically allocate the new string:

char * ranged_copy(const char *start, const char *end)
{
  char *s;

  s = malloc(end - start + 1);
  memcpy(s, start, end - start);
  s[end - start] = 0;

  return s;
}

Upvotes: 0

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23198

Just create a new buffer called dest and use strncpy

char dest[end-start+1];
strncpy(dest,start,end-start);
dest[end-start] = '\0'

Upvotes: 3

Related Questions