0

I have a long string and I want to take specific substrings of it and store it into an array of strings. I tried using malloc() with memcpy() but it doesn't seem to work. how would I go about doing this? code:

for(i = 0; i < strlen(p); i++){
        if(p[i] == ':'){
            cnt++;
            end = i - start;
            list[i] = malloc(1000);
            memcpy( list[i], &p[start], end );
            list[i][end] = '\0';
            //printf("%s\n", list[i]);
            start = i + 1;
        }
    }
7
  • 1
    Post the code that does not work. Commented Mar 22, 2015 at 20:33
  • 1
    Related: stackoverflow.com/questions/26378781/… Commented Mar 22, 2015 at 20:34
  • 1
    @YazanWYusuf That is C++, note the tags. Commented Mar 22, 2015 at 20:40
  • 2
    What are you trying to do? Do you want to split the string on colons? You use the index i for both the char buffer and the substrings, which is certainly wrong. If you know the length of your substring, there's no need to guess the allocated memory to 1000; use malloc(end + 1). Finally, do you really need copies? There might be an easier way to do what you want, for example strtok. Commented Mar 22, 2015 at 20:48
  • 1
    I assume you mean list[count] = malloc(1000), didn't you? The countth substring? Same with all i indices in the loop. Commented Mar 22, 2015 at 20:48

3 Answers 3

1
int len   = 0;
int start = 0;
int cnt   = 0;

for( = 0; i < strlen(p); i++)
{       
    if(p[i] == ':')
    { // then, found delimeter
        len = i - start+1;
        list[cnt] = malloc(len);
        memset( list[cnt],'\0', len );
        memcpy( list[cnt], &p[start], len );

        //printf("%s\n", list[i]);
        start = i + 1;
        cnt++;
    } // end if
} // end for
Sign up to request clarification or add additional context in comments.

Comments

1

One solution could be to use strtok, strncpy, realloc functions. For example:

int main(void){
    char str[] = "asbd0:sdsd1:ssdwdwd2";
    int tokens = 0;
    int i = 0;
    char **res = NULL;

    char *token = strtok(str, ":");
    for (tokens = 1; token; ++tokens) {
        res = realloc(res, tokens *sizeof(*res));       
        res[tokens - 1] = malloc(strlen(token) + 1);
        strncpy(res[tokens - 1], token, strlen(token) + 1);
        token = strtok(NULL, ":");
    }

    for (i = 0; i<tokens-1; ++i){
        puts(res[i]);
    }
    return 0;
}

This way we expand our "array of strings" as needed every time we find another : delimited string.

(coliru)

Comments

0

I suggest you to use strtok or better strtok_r. Use them like this.

char *str = "Hello you new guy";
char *temp;
temp = strtok(str," ");
while(temp!=NULL) {
    printf("%s\n",temp);
    temp = strtok(NULL," ");
}

Be careful!strtok is destructive. Use strtok_r instead :

Just like strtok, this function splits the string into several tokens which can be accessed by successive calls to strtok_r. The difference is that the information about the next token is stored in the space pointed to by the third argument, save_ptr, which is a pointer to a string pointer. Calling strtok_r with a null pointer for newstring and leaving save_ptr between the calls unchanged does the job without hindering reentrancy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.