UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » strtok corrupt the passed char pointer?

#1 2007-09-26 11:16 AM

santoshkumar
Member
Registered: 2007-08-21
Posts: 53

Re: strtok corrupt the passed char pointer?

Offline

#2 2007-09-26 11:37 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: strtok corrupt the passed char pointer?

Better to use strspn() or strchr().

Offline

#3 2007-09-26 01:32 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: strtok corrupt the passed char pointer?

Yes, it looks like you're just trying to obtain the count of the number of ":"-separated
tokens, so using strtok() is major overkill...  If you really wanted to use it, you could
simply strdup() or strdupa() the string first, so you didn't trash it...  However, using
strtok() for something so trivial is silly anyway...  (In fact, I'd say using it at all these
days is fairly silly...  There are far better functions available, with less bizarre
semantics, and which are actually thread-safe...  Hell, it's trivial to just whip up your
own saner replacement with strchr() or strpbrk(), even...)  But, for simply counting
":"-separated tokens, all you need is a strchr() loop:

for (i = 0, ptr = macid; (ptr = strchr (ptr, ':')) != NULL; i++, ptr++) ;
i++;

Note: the extra "i++" outside the loop is because the loop will obtain the number of
":"s in the string, not the number of tokens, which is one greater than that value...

Also, this is very simplistic, in that it doesn't consider multiple ":"s next to each other
and condense them into a single one, as strtok() does...  So, if you want to ignore
empty tokens in your count, you'll have to do a little bit more more work...

Offline

#4 2007-10-05 03:08 PM

santoshkumar
Member
Registered: 2007-08-21
Posts: 53

Re: strtok corrupt the passed char pointer?

Hi thanku for the reply  ,prefer to # define this code as may be used at multiple places.
getting  compilation error!
as macro do not return, i used a ternary , awaiting suggestion


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

#define IS_MACRO_VALID(macid) { \
char* ptr;\
int i;\
for (i = 0, ptr = macid; (ptr = strchr (ptr, ':')) != NULL; i++, ptr++) ; \
i++;  \
((i==6)? 0 : 1); \
}

main()
{
char * macid="11:22:3:4:5:6";
if(IS_MACRO_VALID(macid) != 0)
printf("Invalid Macid");
}

thanks for ur suggestion.

Offline

#5 2007-10-05 07:18 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: strtok corrupt the passed char pointer?

That's invalid macro syntax for a macro with a return value...  Surround the outer
curly-braces with parenthesis...  Ie:

#define IS_MACRO_VALID(macid) ({ \
char* ptr;\
int i;\
for (i = 1, ptr = macid; (ptr = strchr (ptr, ':')) != NULL; i++, ptr++) ; \
((i==6)? 0 : 1); \
})

Note: I also fixed a bit of silly brain-sleepiness on my part last time, by having it
simply start i at 1 instead of 0 and then incrementing after the loop...  It doesn't
really matter, but this way is theoretically very slightly more efficient...  BTW, what
happened to your old length test?  Oh, and you can just simply that return value
down to just: "(i != 6)", too...  Though, it seems odd that you're returning 0 (false)
in the case that it IS valid, and 1 (true) in the case that it's NOT valid...  Sure you
don't want to reverse that?  I'd think making it return a boolean value would make
more sense in this case...  Unless you plan on returning some other error codes
or something, to indicate types of failure later, or something? *shrug*

Also, if using gcc at least, you can make it an inline function and have it be just
as efficient as a macro, but without the quirky macro syntax issues, making it look
just like a regular function...  Eg:

static inline int IS_MACRO_VALID (char *macid)
{
    char* ptr;
    int i;
    for (i = 1, ptr = macid; (ptr = strchr (ptr, ':')) != NULL; i++, ptr++) ;
    return ((i==6)? 0 : 1);
}

Offline

  • Index
  • » C
  • » strtok corrupt the passed char pointer?

Board footer

Powered by FluxBB