OK so how can i have sscanf scan a string with an undetermined number of separators in it, then separate it out into separate strings inside a array of strings.

So essentially, how can I modify this code to make it work?

char * commands; //simple string as pointer
commands = "Commands::System::Windows::Accessibility::";
char ** commands_separated; //array of strings

sscanf(commands,"%s::",commands_separated);

and having the final value of commands_separated being:
commands_separated[0] = "Commands";
commands_separated[1] = "System";
commands_separated[2] = "Windows";
commands_separated[3] = "Accessibility";

Something akin to this?

well I tried using strtok (due to your suggestion) rather than sscanf

I came up with this, but it crashes (but compiles correctly) every time I use it. Any ideas what I can do to fix it?

char **cmds;
char *result = NULL;
result = strtok( buf, "::" );
for(int i = 0; result != NULL; i++ )
{
	cmds[i] = result;
	result = strtok( NULL, "::" );
	MessageBox(NULL,cmds[i],0,0);
}

well I tried using strtok (due to your suggestion) rather than sscanf

:?: I specifically recommend against strtok .

I didn't think it would be that difficult to take that snippet and modify it into this.

#include <stdio.h>

int main(void)
{
   const char line[] = "Commands::System::Windows::Accessibility::";
   const char *ptr = line;
   char field [ 32 ];
   int n;
   while ( sscanf(ptr, "%31[^:]::%n", field, &n) == 1 )
   {
      printf("field = \"%s\"\n", field);
      ptr += n;
   }
   return 0;
}

/* my output
field = "Commands"
field = "System"
field = "Windows"
field = "Accessibility"
*/

Yikes.

I came up with this, but it crashes (but compiles correctly) every time I use it. Any ideas what I can do to fix it?

char **cmds;
char *result = NULL;
result = strtok( buf, "::" );
for(int i = 0; result != NULL; i++ )
{
	cmds[i] = result;
	result = strtok( NULL, "::" );
	MessageBox(NULL,cmds[i],0,0);
}

Your declaration of cmds is just a dangling pointer. If you want to store the strings parsed from the text, you're going to have to allocate space for these strings to exist.

[edit] This was one way with an array.

sorry Sinkula.. I was half asleep when I posted last night.

It works like a charm now that I replaced the pointer junk.

char cmds[BUFSIZ][7];
char *result = NULL;
result = strtok( buf, "::" );
for(int i = 0; result != NULL; i++ )
{
	sprintf(cmds[i],result);
	result = strtok( NULL, "::" );
	MessageBox(NULL,cmds[i],0,0);
}
char cmds[BUFSIZ][7];

Wouldn't you want ... ?

char cmds[7][BUFSIZ];

"Accessibility" is longer than 6 characters.

my bad, its backwords. should be like this.

char cmds[7][BUFSIZ];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.