linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "J." <mailing-lists@xs4all.nl>
To: linux-c-programming@vger.kernel.org
Subject: Re: *w[1]++ = *[k[1]++; does not work .. ?
Date: Mon, 24 Feb 2003 08:55:16 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.21.0302232200530.10835-100000@hestia> (raw)
In-Reply-To: <20030223213034.B21636@neutrino.particles.org>

I am sorry, maybe I am not being to bright about this problem. But I still
do not see the clue.

On Sun, 23 Feb 2003, Elias Athanasopoulos wrote:

> On Sun, Feb 23, 2003 at 06:37:08PM +0100, J. wrote:
> > I can print char by char like this: printf("%c", *w[1]++);
> > but I can not copy char by char like this: *w[1]++ = *k[1]++;
> 
> It works.
> 
> >  while(*w[1] != '\0')
> >   *k[1]++ = *w[1]++; 
> 
> You move the pointer while doing the copy, so after the loop it points to
> the terminated null character.

Yes and then everything is copy'd, including the '\0' terminator just like
strcpy(). So k[1] should point to the character string and *k[1] points
to the first charater of the string ... ? 

> You should keep the original address of k[1] before the loop and use it 
> instead. I.e.:

The original address of *k[1] is preserved, only the memory segment
address it points to is incremented. I think...

> 	char *s;
> 	...
> 	s = k[1];
> 
> 	while (...) { ... }
> 	
> 	printf ("%s\n", s);
> 
> You can do the copy more efficiantly using other ways. Check how strcpy()
> works (it exists in K&R, glibc or even inside the kernel).
>
> Elias

Unfortunatly I can't use strcpy(), because I have to tokenze a string into
several sub-slices. One milion text lines (which is nothing), is prox. 14
milion words, is prox. 49 milion tokens. That is 49 milion strcpy's! So
this part could be classifyd as time-critical. I have a strcpy() version
and `gprof'd it. But the results were not very optimal. I was hoping by
just using pointers I could speed it up a little more.

But this is how my brain percieves this issue... Where do I go wrong ?

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

int main(void) {
             //  ___
 char *k[1]; // | 0 | -> 
             // |___|
             // | 1 | ->
             // |___|
             //  ___
 char *w[1]; // | 0 | ->
             // |___|
             // | 1 | ->
             // |___|

 char str[] = "jehova";
 /*
 --- --- --- --- --- --- ---
| j | e | h | o | v | a |\0|
 --- --- --- --- --- --- ---
 */

 w[1] = strdup(str);
 /*
  ___      --- --- --- --- --- --- ---
 | 0 | -> | j | e | h | o | v | a |\0|
 |___|     --- --- --- --- --- --- ---
 | 1 | -> NULL;
 |___|
 */

 k[1] = calloc(strlen(str) + 1, sizeof(char));
 /*
  ___      --- --- --- --- --- --- ---
 | 0 | -> |\0 |\0 |\0 |\0 |\0 |\0 |\0|
 |___|     --- --- --- --- --- --- ---
 | 1 | -> NULL;
 |___|
 */

 // Now for the copy part...
 // Eh.. ?
 while(*w[1] != '\0')
  *k[1]++ = *w[1]++; 

 // now k[1] should be:
 /*
  ___      --- --- --- --- --- --- ---
 | 0 | -> | j | e | h | o | v | a |\0|
 |___|     --- --- --- --- --- --- ---
 | 1 | -> NULL;
 |___|
 */

 // prints nothing....
 printf("%s\n", w[1]);
 printf("%s\n", k[1]);

 return 0;
}

Thank you for your patience...

J.


  reply	other threads:[~2003-02-24  7:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-23 17:37 *w[1]++ = *[k[1]++; does not work .. ? J.
2003-02-23 19:30 ` Elias Athanasopoulos
2003-02-24  7:55   ` J. [this message]
2003-02-24  9:05     ` Elias Athanasopoulos
2003-02-24  9:35       ` *w[1]++ = solved... thnkx... ? J.
2003-02-24 16:02 ` *w[1]++ = *[k[1]++; does not work .. ? IVAN DE JESUS DERAS TABORA
  -- strict thread matches above, loose matches on Subject: below --
2003-02-24  8:21 Alvarez Alberto-AALVARB1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0302232200530.10835-100000@hestia \
    --to=mailing-lists@xs4all.nl \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).