Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #101695

strtok() and the mistery of the same address strings with different lengths...

Newsgroups comp.lang.c
Date 2017-01-13 16:16 -0800
Message-ID <94292e94-bcb9-40ea-8b8a-2a8563eeb33e@googlegroups.com> (permalink)
Subject strtok() and the mistery of the same address strings with different lengths...
From "Fernando D. Bozzo" <fdbozzo@gmail.com>

Show all headers | View raw


Ok, this is driving me crazy since 2 hours now.

Given this program that parses the string stored in "line[]":


/**
 * parse-0.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char *argv[])
{
    char line[] = "GET /this%20is%20a/page.html?q=cats HTTP/1.1";
    char *abs_path = NULL;
    char *query = NULL;
    //
    printf("line = [%s], address = %p\n", line, line);
    char *cmd = strtok(line, " ");
    printf("cmd = [%s], address = %p\n", cmd, cmd);
    char *PathAndQuery = strtok(NULL, " ");
    printf("PathAndQuery = [%s], address = %p\n", PathAndQuery, PathAndQuery);
    //
    char *Http = strtok(NULL, " ");
    printf("Http = [%s], address = %p\n", Http, Http);
    abs_path = strtok(PathAndQuery, "?");
    printf("abs_path = [%s], address = %p\n", abs_path, abs_path);
    query = strtok(NULL, "?");
    printf("query = [%s], address = %p\n", query, query);
    //
    printf("cmd = [%s], address = %p\n", cmd, cmd);
    
    return 0;
}


...How is possible that the function strtok() could return different strings --each with it's own \0 termination-- in different string variables without dinamic memory?

This is the generated output:

fernando@fer-desktop:~/DESA/C-Lang/Tests$ ./parse-0 
line = [GET /this%20is%20a/page.html?q=cats HTTP/1.1], address = 0x7ffc120c8240
cmd = [GET], address = 0x7ffc120c8240
PathAndQuery = [/this%20is%20a/page.html?q=cats], address = 0x7ffc120c8244
Http = [HTTP/1.1], address = 0x7ffc120c8264
abs_path = [/this%20is%20a/page.html], address = 0x7ffc120c8244
query = [q=cats], address = 0x7ffc120c825d
cmd = [GET], address = 0x7ffc120c8240


I have printed out "cmd" twice to make sure that the value is really there.

Look at the addresses of "PathAndQuery" and "abs_path" (0x7ffc120c8240), they are the same! How is possible that 2 vars point at the same address and have different length at the same time?

I have no explanation for this...  :-/

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 16:16 -0800
  Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 16:30 -0800
  Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 00:44 +0000
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 17:43 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... Manfred <noname@invalid.add> - 2017-01-14 16:52 +0100
      Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 19:38 +0000
  Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-13 16:49 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 17:39 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-13 22:00 -0500
        Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 03:11 -0800
          Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-14 10:50 -0500
            Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:27 -0800
              Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:21 -0800
              Re: strtok() and the mistery of the same address strings with different lengths... Les Cargill <lcargill99@comcast.com> - 2017-01-15 11:48 -0600
                Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-15 15:24 -0800
                Re: strtok() and the mistery of the same address strings with different lengths... Les Cargill <lcargill99@comcast.com> - 2017-01-15 22:11 -0600
      Re: strtok() and the mistery of the same address strings with different lengths... Manfred <noname@invalid.add> - 2017-01-14 16:48 +0100
  Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 03:18 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 04:16 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 15:14 +0000
        Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 09:58 -0800
          Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 19:51 +0000
            Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:18 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:17 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 15:09 +0000
      Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 11:09 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... luser droog <luser.droog@gmail.com> - 2017-01-14 11:41 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 20:04 +0000
        Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-14 15:09 -0500
          Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:22 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:21 -0800

csiph-web