Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why pointer to string changes its content after being used in the strtok function?

This part is confusing me a bit. When i run the program an use "one two three" as input the first print of the buffer is a-ok. Once i use strtok and try to print the buffer again it only prints "one" and not "one two three". Have i changed the content of the buffer without knowing? If yes is there a way for that not to happen?
Thank you for your time!

#include "stdlib.h"
#include "string.h"

int main (void )
{
   char *buffer;
    size_t buffer_size =64;
    buffer = (char *)  malloc(64 * sizeof (char ));
    getline(&buffer,&buffer_size,stdin);
    char *anotherbuffer;
    printf("%s\n",buffer);
    anotherbuffer = (char *) malloc(64 *sizeof (char ));
    anotherbuffer = strtok(buffer," ");
    anotherbuffer = strtok(NULL," ");
    printf("The buffer \"buffer\" containts %s\n",buffer);
    printf("The buffer \"anotherbuffer\" containts %s\n",anotherbuffer);
    return 0;
}```

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you look at the description of strtok (for example on cppreference.com) you will find that it puts a ‘\0’ byte at the first (or on subsequent calls, next) separator that it detects. That’s why when you print your buffer after the first use of strtok, the C string in that buffer has actually gotten shorter. The rest of the characters is still there, but as far as printf %s is (and other string functions like strlen are) concerned, the string is now terminated where the first separator used to be, because there is a ‘\0’ character now.

To avoid modifying your buffer, you need to use different functions. For example, you could use strpbrk or strcspn to find the address of the first separator from one memory address, or the length up to that first separator, respectively. From there you could move forwards by using the new address as the starting point in the next calls to these functions. To extract a token into its own C string, you would have to copy it out of the original buffer into a new one, and terminate it with a \0 there.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading