2

I already have a function to get substring of a string

void subString(char* buffer, char* str, int start, int length)
{
    int i, x = 0;
    int end=start+length;
    for(i = start ; i <= end; i++)
        buffer[x++] = str[i];
    buffer[x] = '\0';

    //return temp;
}

new string is stored in buffer

but I prefer the function likes

char * subString(char* str, int start, int length)
{

    //.......

}

it will automatically returns the string pointer that has been alloced memory.

Welcome any comment

monsabre
  • 2,099
  • 3
  • 29
  • 48
  • your `i <= (start+length)` is sospicious (wrong) e.g. "HELLO" with start=0 and length=2 should pick HE but i goes from 0 to 2 included, that is, it takes "HEL" – ShinTakezou Jul 17 '11 at 20:46
  • I thought the question was about the pros and cons of both function prototypes, but answers below are providing an implementation for the second one instead. It really is unclear what is asked. Voting to close. – Pascal Cuoq Jul 17 '11 at 20:52

2 Answers2

3

What's wrong with just adding the malloc into your new function and leaving the rest the same?

#include<stdlib.h>
char * subString(char* str, int start, int length) {
    char *newString = (char *)malloc(length * sizeof(char));
    int i, x = 0;
    int end=start+length;
    for(i = start ; i <= end; i++)
        newString[x++] = str[i];
    newString[x] = '\0';
    return newString;
}
Chris Gregg
  • 2,376
  • 16
  • 30
  • Nice, but don't cast the return value of `malloc` and don't take the size of `char` :-) – cnicutar Jul 17 '11 at 20:50
  • Why not? Being explicit doesn't reduce the clarity of the statement and it reinforces correct usage of `malloc`. – Alex Reynolds Jul 17 '11 at 20:53
  • @cnicutar -- I see the topic has been closed, but I don't follow why I wouldn't cast `malloc` or use `sizeof(char)`? Without casting in `gcc`, you get a warning (incompatible implicit definition of malloc) and without the sizeof, you may get a different size of char depending on architecture (although I agree that sizeof(char) is almost always 1). – Chris Gregg Jul 17 '11 at 20:53
  • @Chris Gregg This isn't C++. It's C :-) http://c-faq.com/malloc/cast.html and http://c-faq.com/malloc/mallocnocast.html . In C `sizeof(char) == 1` **always** (standard says so). – cnicutar Jul 17 '11 at 20:55
  • @Alex Reynolds See the link I posted – cnicutar Jul 17 '11 at 20:55
  • @cnicutar -- fine, but you still need the cast in `gcc`. – Chris Gregg Jul 17 '11 at 20:56
  • @Chris Gregg No, you don't :) – cnicutar Jul 17 '11 at 20:57
  • @cnicutar -- well then I guess you just like warnings. :) – Chris Gregg Jul 17 '11 at 20:58
  • @Chris Gregg `gcc` shouldn't warn you when not casting malloc. Are you sure you're not using C++ ? – cnicutar Jul 17 '11 at 20:59
  • `$ gcc substr.c substr.c: In function ‘subString’: substr.c:5: warning: incompatible implicit declaration of built-in function ‘malloc’` – Chris Gregg Jul 17 '11 at 21:00
  • @Chris Gregg http://stackoverflow.com/questions/1230386/why-do-i-get-a-warning-everytime-i-use-malloc :-)) – cnicutar Jul 17 '11 at 21:02
  • @cnicutar -- my mistake! I took out the `#include` to make sure it was needed; thanks for setting me straight! :) – Chris Gregg Jul 17 '11 at 21:07
1

Since you are treating strings as simple character arrays, why don't you simply use plain old strncpy?

strncpy(dest, str+offset, len);
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281