0

I think I have implemented most of the parse function in server but I don't understand character pointers well enough to finish I think. I know the strchr function finds the character in the string and returns a pointer to the rest of the string starting at that character but I don't know how to get a string that only points to the part of the string that ends in a character. I think maybe I have to copy it to an array but I'm not 100% sure on how I would do that. Or can you access character inside a pointer the same way as an array? And can you use strlen on it? here is my card code which passes too many characters into abs_path and query:

    bool parse(const char* line, char* abs_path, char* query)
    {
       //make sure method request is get
    const char* get = "GET /";
    if (strncmp(get, line, 4) != 0)
    {
    error(405);
    return false;
    }
    //make sure request Target begins with slash
    if (strncmp(get, line, 5) != 0)
    {
    error(501);
    return false;
   }

    const char* qu = '"';
    const char* qm = '?';
    const char* sp = ' ';
    const char* sl = '/';
    //separate requests Target
     char *rt  = strchr (line, sp);
    //get absolute path path
     abs_path = strchr (rt, sl);
    // get query
    if (strchr (rt, qm) == NULL)
   query = '/0';

   else
  {
   query = strchr (rt, qm);
  }
    //check for HTTP 1.1
   char *rst  = strchr (line, sp);
   char *http = ' HTTP/1.1';

    if (strstr(rst, http) == NULL)
   {
           error(505);
    return false;
   }
   //check for the other thing
      char *http = 'HTTP/1.1\r\n';
         if (strstr(rst, http) == NULL)
   {
           error(505);
    return false;
   }
    //check for quotation marks in request
     if (strchr( rt, qu) != NULL)
   {
    error(400);
    return false;
   }

     }
0

in the background all are chain, including arrays are handled by pointer but in a special way since we can not change its memory address, while a pointer we can, in fact if we say char array [], the name itself is a pointer, so we can use the pointer notation and say

char* pointer;

pointer = array (both pointers point to the same location)

*(pointer + 1) is equivalent to array [1], while (pointer + 1) is pointer to array [1], reciprocally if we have a pointer (created with strchr for example) const char * qu, you can access the other elements (if any) with array notation and say, qu[0], qu[1] etc,I cannot remember if strchr copies the null character, condition without which an array it is not a chain

You must log in to answer this question.

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