String token from `strsep` does not print (seg fault)

I am using a smaller piece of code to test the functionality for a larger (beginner) program, but I have a problem displaying the marker that I pulled from the line.

I found and used:

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

int main()
{

char *string, *found;

string = strdup ("1/2/3");
printf("Original string: '%s'\n",string);

while ((found = strsep(&string,"/")) != NULL )
  printf ("%s\n",found);

return (0);
}

and it works great, prints the tokens one at a time as lines.

Then, when I try to jump to the line entered by the user:

#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"); /*To pinpoint where the seg fault arises*/
    printf("%s\n",found);

  return(0);
}

I get a seg error in a string printf("%s\n",found);. I get information on the basics of pointers, arrays, and strings, but it’s clear that I missed something, and I would like someone to tell me what it is.

- printf("%s\n",found);, . to printf("%i\n",found); , , . 1/2/3, , 1111/2222 . % c,% i,% d,% p, , % s seg faults.

.

+1
2

Segfault - , while. " 1", strsep NULL, ( segfault).

(, -Wall), gcc :

sep.c:13:3: warning: thiswhile’ clause does not guard... [-Wmisleading-indentation]
   while((found =  strsep(&cp,"/,-")) != NULL )
   ^~~~~
sep.c:15:5: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘while
     printf("%s\n",found);
     ^~~~~~

, while, :

./sep 

Enter string: abc/def
Original string: 'abc/def'
Test 1abc
Test 1def
+3

:

while((found =  strsep(&cp,"/,-")) != NULL )
    printf("Test 1"); /*To pinpoint where the seg fault arises*/
    printf("%s\n",found);

, printf ,

while((found =  strsep(&cp,"/,-")) != NULL )
{
    printf("Test 1"); /*To pinpoint where the seg fault arises*/
}
    printf("%s\n",found);

, printf("%s\n",found); printf("%s\n",NULL); undefined segfault.

, C . { } :

while((found =  strsep(&cp,"/,-")) != NULL )
{
    printf("Test 1"); /*To pinpoint where the seg fault arises*/
    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   // <-- important
#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"); /*To pinpoint where the seg fault arises*/
    printf("%s\n",found);
    }

  return(0);
}

:

+2

Source: https://habr.com/ru/post/1695118/


All Articles