linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* strtok_r crashing
@ 2002-11-14 12:46 Hemant Mohan
  2002-11-14 12:50 ` Jan-Benedict Glaw
  0 siblings, 1 reply; 5+ messages in thread
From: Hemant Mohan @ 2002-11-14 12:46 UTC (permalink / raw)
  To: linux-c-programming

Hi,

I am trying to parse a string using strtok_r (I need reentrant code) as
follows:

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

int main()
{
  char *str = NULL;
  char *temp = NULL;
  char **ptr = NULL;   // Note that this is a char**
  int x = 0;
  int y = 0;

  str = malloc (10*sizeof(char));
  sprintf ( str,"%s", "320x240");
  temp = strtok_r ( str, "Xx", ptr );
  x = atoi(temp);
  temp = strtok_r ( NULL, "Xx", ptr);
  y = atoi(temp);
  printf("x = %d\t y= %d\n", x, y);

 return 0;
}


This code receives a SIGSEGV in strtok_r.

However, if I change the declaration of ptr from char** to char* and change 
the third arguement of strtok_r to  &ptr it works.
That is
char* ptr = NULL;
temp = strtok_r ( str, "Xx" &ptr);

Can anyone please help me know the difference between these two calls
as I think both calls are similar.

Thanks,
Hemant Mohan

-------------------------------------------------------

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-11-15  6:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-11-14 12:46 strtok_r crashing Hemant Mohan
2002-11-14 12:50 ` Jan-Benedict Glaw
2002-11-15  5:45   ` Hemant Mohan
2002-11-15  6:26     ` Glynn Clements
2002-11-15  6:42       ` Hemant Mohan

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).