tags:

views:

168

answers:

2

Possible Duplicate:
How to copy char *str to char c[] in C?

char *token = "some random string";
char c[80];  
strncpy(c, token, sizeof c - 1); 
c[79] = '\0';
char *broken = strtok(c, "#");
+1  A: 

Your code does not crash for me in the following:

#include <string.h>

main()
{
char *token = "some random string";
char c[80];  
strcpy( c, token);
strncpy(c, token, sizeof c - 1); 
c[79] = '\0';
char *broken = strtok(c, "#");
}
Sean A.O. Harney
he doesn't use: strcpy( c, token);Perhaps you could be clearer why this extra step is critical?
Egwor
@Egwor, in the earlier revisions it was the same. I suppose that is what was causing the problem in cases where token pointed to a string which was too long.
Sean A.O. Harney
A: 

This code works, have you specified the correct includes?

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

int
main() {
  /* ORIGINAL CODE */
  char *token = "some random string";
  char c[80];  
  strcpy( c, token);
  strncpy(c, token, sizeof c - 1); 
  c[79] = '\0';
  char *broken = strtok(c, "#");

  /* ADDED THE FOLLOWING LINES */
  printf("%s\n", broken);
  exit(1);
}
Eineki