Код
#include <string.h>
int main()
{
char str[] = "192.168.0.1";
unsigned char bytes[4];
int i = 0;
char* buff = malloc(10);
buff = strtok(str,".");
while (buff != NULL)
{
//if you want to print its value
printf("%s\n",buff);
//and also if you want to save each byte
bytes[i] = (unsigned char)atoi(buff);
buff = strtok(NULL,".");
i++;
}
free(buff);
return 0;
}
int main()
{
char str[] = "192.168.0.1";
unsigned char bytes[4];
int i = 0;
char* buff = malloc(10);
buff = strtok(str,".");
while (buff != NULL)
{
//if you want to print its value
printf("%s\n",buff);
//and also if you want to save each byte
bytes[i] = (unsigned char)atoi(buff);
buff = strtok(NULL,".");
i++;
}
free(buff);
return 0;
}
Чуть-чуть переделал его под себя
Код
char sc[] = "150.156.16";
byte bytes[3] = " ";
int i = 0;
char* buff;
buff = strtok(sc,(const char*)'.');
while (buff != NULL)
{
bytes[i] = (byte)atoi(buff);
buff = strtok(NULL,(const char*)'.');
i++;
}
byte bytes[3] = " ";
int i = 0;
char* buff;
buff = strtok(sc,(const char*)'.');
while (buff != NULL)
{
bytes[i] = (byte)atoi(buff);
buff = strtok(NULL,(const char*)'.');
i++;
}
Но считывается только первое число (150), а дальше происходит выход из цикла, и я не пойму, почему. Подскажите, пожалуйста, какие условия из описания я не выполнил (P.S. ниже я привел описание функции, взятое из самого компилятора)?
Цитата
/** @name strtok
* ``A sequence of calls to the {\bf strtok} function breaks the
* string pointed to by {\bf s1} into a sequence of tokens, each of
* which is delimited by a character from the string pointed to by
* {\bf s2}. The first call in the sequence has {\bf s1} as its
* first argument, and is followed by calls with a null pointer
* as their first argument. The separator string pointed to by {\bf s2}
* may be different from call to call.
*
* ``The first call in the sequence searches the string pointed to
* by {\bf s1} for the first character that is {\it not} contained in
* the current separator string {\bf s2}. If no such character is found,
* then there are no tokens in the string pointed to by {\bf s1} and the
* {\bf strtok} function returns a null pointer. If such a character is
* found, it is the start of the first token.
*
* ``The {\bf strtok} function then searches from there for a character
* that {\it is} contained in the current separator string. If no such
* character is found, the current token extends to the end of the
* string pointed to by {\bf s1}, and subsequent searches for a token
* will return a null pointer. If such a character is found, it is
* overwritten by a null character, which terminates the current token.
* The {\bf strtok} function saves a pointer to the following character,
* from which the next search for a token will start.
*
* ``Each subsequent call, with a null pointer as the first argument,
* starts searching from the saved pointer and behaves as described
* above.
*
* ``The implementation shall behave as if no library function calls the
* {\bf strtok} function.
*
* ``This function is implemented in C, is not re-entrant and calls the
* functions: strspn, strpbrk, memchr.''
*
* @param s1 pointer to a string to begin searching, or null to continue
* searching a prior string
* @param s2 pointer to a string containing the set of separator characters
* @return ``The {\bf strtok} function returns a pointer to the first
* character of a token, or a null pointer if there is no token.''
*/
char *strtok (auto char *s1, auto const char *s2);
* ``A sequence of calls to the {\bf strtok} function breaks the
* string pointed to by {\bf s1} into a sequence of tokens, each of
* which is delimited by a character from the string pointed to by
* {\bf s2}. The first call in the sequence has {\bf s1} as its
* first argument, and is followed by calls with a null pointer
* as their first argument. The separator string pointed to by {\bf s2}
* may be different from call to call.
*
* ``The first call in the sequence searches the string pointed to
* by {\bf s1} for the first character that is {\it not} contained in
* the current separator string {\bf s2}. If no such character is found,
* then there are no tokens in the string pointed to by {\bf s1} and the
* {\bf strtok} function returns a null pointer. If such a character is
* found, it is the start of the first token.
*
* ``The {\bf strtok} function then searches from there for a character
* that {\it is} contained in the current separator string. If no such
* character is found, the current token extends to the end of the
* string pointed to by {\bf s1}, and subsequent searches for a token
* will return a null pointer. If such a character is found, it is
* overwritten by a null character, which terminates the current token.
* The {\bf strtok} function saves a pointer to the following character,
* from which the next search for a token will start.
*
* ``Each subsequent call, with a null pointer as the first argument,
* starts searching from the saved pointer and behaves as described
* above.
*
* ``The implementation shall behave as if no library function calls the
* {\bf strtok} function.
*
* ``This function is implemented in C, is not re-entrant and calls the
* functions: strspn, strpbrk, memchr.''
*
* @param s1 pointer to a string to begin searching, or null to continue
* searching a prior string
* @param s2 pointer to a string containing the set of separator characters
* @return ``The {\bf strtok} function returns a pointer to the first
* character of a token, or a null pointer if there is no token.''
*/
char *strtok (auto char *s1, auto const char *s2);