I am using strtok() to split request_line into multiple variables. However, request_line is not being split correctly and I am receiving errors caused by using strtok().

How can I use strtok() to split request_line? I read that after the first use of strtok(), I'd have to use it with NULL. Why would I have to use NULL?

Thanks in advance.


I rwote multiple test programs to see how the functions work, you can try the following to see how to use strtok to separate the different parts of request line

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void printChars(char* string, int len);

int main()
    char request[] = "GET /favicon.ico HTTP/1.1\r\n";
    int len = strlen(request);
    char* req[3];

    printf("request:  %s\n", request);
    req[0]=strtok(request, " ");
    printChars(request, len);
    req[1]=strtok(NULL, " ");
    printChars(request, len);
    req[2]=strtok(NULL, "\r\n");
    printChars(request, len);
    printf("req[0]: %s\n", req[0]);
    printf("req[1]: %s\n", req[1]);
    printf("req[2]: %s\n", req[2]);

    return 0;

void printChars(char* string, int len)
    for (int i = 0; i < len; i++)
        if (string[i] == 0)
            printf("%c", string[i]);

EDIT Strictly this corresponds to another question (this is how this site works), I thought that you were already able to use GDB by yourself, when debugging parse we obtain the following message:

Program received signal SIGSEGV, Segmentation fault.
__strlen_sse2_bsf () at ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S:50
50  ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S: No such file or directory.

This happens in the next line:

query_length = strlen(query); 
  • Is difficult to know without seeing the code, error messages can serve but pset6 is very subtle in the treatment of strings, is it possible to publish the code along with errors?
    – MARS
    Jan 5 '17 at 21:24
  • You can read my edit, please if there are more questions start a new thread
    – MARS
    Jan 6 '17 at 8:53
  • Thank you for this! Jan 6 '17 at 13:04
  • I have to apologize if you feel offended by my words, at no time was my intention, what I want to say is that this way of solving each problem is very negative for you, if you do not fight enough you can not learn to program, it is need to ask about a problem when you have struggled for a while without finding a way out, I hope you take up the course and learn many things, a greeting
    – MARS
    Jan 8 '17 at 10:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .