Reputation: 11
char WSOH_SO_ID[11]; --> value is 3723611
char* FPART_SO_ID = "";
FPART_SO_ID = substr(WSOH_SO_ID, 0, 5);
char* substr(const char *src, int m, int n)
{
// get length of the destination string
int len = n - m;
// allocate (len + 1) chars for destination (+1 for extra null character)
char *dest = (char*)malloc(sizeof(char) * (len + 1));
// start with m'th char and copy 'len' chars into destination
strncpy(dest, (src + m), len);
// return the destination string
return dest;
}
Upvotes: 0
Views: 53
Reputation: 12742
strncpy(dest, (src + m), len);
You will have to explicitly terminate the dest
.
Read::
The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
Upvotes: 2