:
while((found = strsep(&cp,"/,-")) != NULL )
printf("Test 1");
printf("%s\n",found);
, printf ,
while((found = strsep(&cp,"/,-")) != NULL )
{
printf("Test 1");
}
printf("%s\n",found);
, printf("%s\n",found); printf("%s\n",NULL);
undefined segfault.
, C .
{ } :
while((found = strsep(&cp,"/,-")) != NULL )
{
printf("Test 1");
printf("%s\n",found);
}
,
$ ./a
Enter string: aa/bb/cc/dd
Original string: 'aa/bb/cc/dd'
Test 1aa
Test 1bb
Test 1cc
Test 1dd
, - ,
, strdup. :
#include <stdio.h>
#include <stdlib.h> // for the free function
#include <string.h>
int main()
{
char *orig = *string, *found;
orig = string = strdup ("1/2/3");
printf("Original string: '%s'\n",string);
while ((found = strsep(&string,"/")) != NULL )
printf ("%s\n",found);
free(orig);
return 0;
}
, , ,
. OP onlinegdb.com
, segfault.
ideone.com, segfault.
, man strsep :
man strsep
#include <string.h>
char *strsep(char **stringp, const char *delim);
glibc (. feature_test_macros (7)):
strsep():
Since glibc 2.19:
_DEFAULT_SOURCE
Glibc 2.19 and earlier:
_BSD_SOURCE
: glibc 2.19: _DEFAULT_SOURCE
,
#define _DEFAULT_SOURCE
C, onlinegdb.com
ideone.com.
, :
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <string.h>
int main()
{
char string[13];
char *found, *cp = string;
fprintf(stderr, "\nEnter string: ");
scanf("%12s",string);
printf("Original string: '%s'\n",string);
while((found = strsep(&cp,"/,-")) != NULL )
{
printf("Test 1");
printf("%s\n",found);
}
return(0);
}
: