LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   strtok corrupt the passed data? (https://www.linuxquestions.org/questions/programming-9/strtok-corrupt-the-passed-data-587453/)

Santoshkb 09-26-2007 05:12 AM

strtok corrupt the passed data?
 
Hi all,
Calling the function is_macid_valid(char * macid), modifies the
macid pointer hence unavailiable for further, i think problem lyis
using strtok, can i have suggestion , so
as to retain the macid value after calling is_macid_valid()..
Thanks in advance



int IS_MACID_VALID(char * macid)
{ int i = 0, len = 0; char *ptr;
len = strlen(macid);
ptr = strtok(macid, ":");
while(ptr) {
ptr = strtok(NULL, ":");
i++;
}
return (((len == 17) && (i==6))? 0 : -1);
}
//return((i==6)? 0 : 1);\

//if(i==6) printf("valid = %d\n", i);\
//else printf("Invalid=%d\n", i); \

main()
{
char san[24]="00:11:22:33:44:55";
printf("Min strlen = %d\n", strlen(san));
printf("Macid Before = %s\n", san);
printf("GG=%d\n",IS_MACID_VALID(san));
printf("After Macid = %s\n", san);
return 0;

Wim Sturkenboom 09-26-2007 10:33 AM

From the man page
Quote:

Bugs
Avoid using these functions. If you do use them, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
The identity of the delimiting character is lost.
The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.
So either save a copy of the original or write your own function; I use 2 pointers and restore the delimiter on successive calls.

PS There is a dedicated programming section at LQ.

jtshaw 09-26-2007 08:03 PM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.


All times are GMT -5. The time now is 04:22 AM.