Visualizzazione dei risultati da 1 a 4 su 4

Discussione: [C] strtok

  1. #1

    [C] strtok

    The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.

    For example:

    char str[] = "now # is the time for all # good men to come to the # aid of their country";
    char delims[] = "#";
    char *result = NULL;
    result = strtok( str, delims );
    while( result != NULL ) {
    printf( "result is \"%s\"\n", result );
    result = strtok( NULL, delims );
    }
    sarò scemo ma qualcosa non mi quadra


    perchè nel while viene passato NULL come stringa?
    Mi aspettavo di dover fare chiamate successive ed identiche e che queste restituissero token successivi.. ma pare non essere così [mi sa che sono abituato troppo comodo con php ]

    qualcuno mi spiega un po' meglio come funge?

    tnx


  2. #2
    l'ho scoperto oggi anchio scrivendo
    questa
    praticamente dopo la prima volta che fai strtok
    se gli passi null lui finisce di parsarti la stringa..

  3. #3
    Originariamente inviato da kentaromiura
    l'ho scoperto oggi anchio scrivendo
    questa
    praticamente dopo la prima volta che fai strtok
    se gli passi null lui finisce di parsarti la stringa..


    è un po' controintuitivo però..

    quindi fammi capire:
    char str[] = "ciao prova";
    char delims[] = " ";
    char *temp = NULL;
    temp = strtok( str, delims );
    printf("%s",temp); //stampa ciao
    temp = strtok( NULL, delims );
    printf("%s",temp); //stampa prova

    giusto?

    vabbè.. mo' provo
    grazie


  4. #4
    Originariamente inviato da }gu|do[z]{®©


    è un po' controintuitivo però..
    vallo a spiegare a quelli dell' ANSI

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.