Working on parse function in Pset6. I have started by creating some pointers to known characters in the request-line:

bool parse(const char* line, char* abs_path, char* query)
const char* sp1 = strchr(line, " ");
const char* sp2 = strrchr(line, " ");
const char* sl1 = strchr(line, "/");
const char* quer = strchr(line, "?");

My question is, how do I read the portions of the request-line between these pointers? For instance, I want to check if method is GET. Does a pointer already exist to the beginning of const char* line? If so, is there a way to read from the beginning of line and stop at sp1 so that I can check if it is = to GET? Likewise, the same goes for reading between sl1 and sp2. I have read the man pages for strstr, strcpy, and memcpy and am not sure how to use them unless I can first read between the pointers. Any help understanding reading and manipulating strings or a good reference explanation would be great. So far, my internet searches have not been very fruitful. Most guides only tell how to access/read/copy a known sub-string between 2 pointers.


Yeah, too bad there's no substr function in C. And I see that if one searches the web for "C substr", one finds a lot of articles that, as you say, "tell how to access/read/copy a known sub-string between 2 pointers". You need to fill the gap between the information you know and the information you need.

Here's a trick if you know 2 pointers and want to discover what is between them. Think about strncpy or memcpy. What do those functions need as input? They need an n that is a size. And what is a size? It is the difference between a start point and an end point. From your example above, what are sp2 and sp1? sp1 is a start point (the first space), sp2 is an end point (the last space) so sp2 - sp1 is a "size". Once you have a "size", a lot of possibilities are available, like strncpy and memcpy, or a for loop, or other stuff I haven't even thought of.

Obviously, this doesn't get into the nitties and the gritties of allocation and null-termination, etc, but hopefully it generates a 💡 moment.

And finally:

Does a pointer already exist to the beginning of const char* line?

Yes. line is a pointer to the beginning of line.

| improve this answer | |
  • Thank you. One more question. For an int of size n, do I need to set it to an arbitrary value that will allocate enough memory to hold whatever may be between the two pointers or does sp2-sp1 literally give back an int n of hte correct size? – Flesheaters Dec 6 '16 at 4:08
  • It must not be correct since I get back the following compiler error when I tried it: server.c:679:17: error: incompatible integer to pointer conversion initializing 'const char ' with an expression of type 'long' [-Werror,-Wint-conversion] const char method = (sp1 - line); – Flesheaters Dec 6 '16 at 5:11
  • First comment: "sp2-sp1 literally give back an int". It literally gives back a long as evidenced by the compile error in comment 2. Re: comment 2: are you missing the keyword malloc? Don't forget space for the null-terminator. – DinoCoderSaurus Dec 6 '16 at 12:24

You must log in to answer this question.

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