www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/19/18:29:39

Date: Tue, 19 May 1998 18:20:37 GMT
From: Junior System Administrator <pete AT restall DOT net>
Message-Id: <199805191820.SAA00355@restallnet.demon.co.uk>
To: djgpp AT delorie DOT com
Subject: Please help me :-)

Hi.  I'm having a problem with some C code to extract tokens from a string.  The
code looks something like:

-------------------->8 <CUT HERE> 8<--------------------
#include <stdio.h>
#include <string.h>


/* Function prototypes */
void	str_gettokens(char *string, char *buf[], int *tokens);


/* Global variables */
#define TOKEN_DELIMITERS	" ,;\t\n\0"



                              /*** Main body ***/

void main(void)
{
	char cmd_string[255];
	char token_array[16][255];	/* Max 16 tokens, 255 chars each */

	int i, token;


	/* Print the available delimiters and prompt for an input */

	printf("The delimiters are: '%s'\n\n", TOKEN_DELIMITERS);
	printf("Enter a command string: ");
	gets(cmd_string);
	str_gettokens(cmd_string, token_array, &tokens);


	/* Print out the tokens */

	for (i = 0; i < tokens; i++)
		printf("%d> %s\n", i, token_array[i]);
	printf("\n%d tokens were retrieved.\n\n", tokens);
}


/*
   void str_gettokens(char *string, char *buf[], int *token):

   This function splits the string '*string' up and puts the different tokens
   (or parts) into the '*buf' array.  The array _MUST_ be the correct size, or
   greater than, as this routine does no boundary checks.

   This function also returns the amount of tokens retrieved in the '*tokens'
   integer pointer.
*/

void str_gettokens(char *string, char *buf[], int *token)
{
	char *p = string;

	int i;


	(*tokens) = 0;
	while (*p != '\0') {
		i = 0;
		while (strchr(TOKEN_DELIMITERS, *p))
			p++;
		while (!strchr(TOKEN_DELIMITERS, *(p + i)))
			i++;


		/* Put the token in the appropriate buffer and advance the
		   token counter */

		printf("%.*s\n", i, p);  /* JUST FOR DEBUGGING -- THIS PRINTS
		                            OUT THE CORRECT VALUES. */
		sprintf(buf[(*tokens)++], "%.*s", i, p);
		p += i;
	};
}
-------------------->8 <CUT HERE> 8<--------------------


When I compile this program, I get a 'suspicious pointer conversion' error from
DJGPP (line that calls 'str_gettokens()').  When I run the compiled EXE, the
program crashes.  However, if I compile and run the program minus the line
that 'sprintf()'s the token into the buffer, the program runs OK (but obviously
without putting anything into 'buf').  I've never had to do anything which
involves passing arrays of character arrays to a function before, and now I'm
a bit lost ;-).  Can anyone help ???

Thanks in advance,


	Pete =:-)


/*************** [burdock] ****************/
/* ____/|   pete AT restall DOT net              */
/* \ o.O|   http://www.restall.net/petes/ */
/*  =(_)=                                 */
/*    U     [FORGET THE ABOVE URL]        */
/******************************************/

/******************************* [Windoze 95] ********************************/
/* Windows 95: n.                                      (Author Unknown).     */
/*    32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit */
/*    operating system originally coded for a 4 bit microprocessor, written  */
/*    by a 2 bit company that can't stand 1 bit of competition.              */
/*****************************************************************************/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019