segmentation fault
9 отговора
272 прегледа
segmentation fault
...
void test(char *buf)
{
char *s;
s = strtok(buf," ");
return ;
}
...
test("ala bala nitza...");
po si4ki pravila tva trqbva da ba4ka, da da ama ne...
nqkakva sveja ideq???!!! ne6to mnogo tapo e ama az kat ne se se6tam
void test(char *buf)
{
char *s;
s = strtok(buf," ");
return ;
}
...
test("ala bala nitza...");
po si4ki pravila tva trqbva da ba4ka, da da ama ne...
nqkakva sveja ideq???!!! ne6to mnogo tapo e ama az kat ne se se6tam
Re: segmentation fault
# man strtok
---
BUGS
Never use this function. This function modifies its first argument. The iden<AD>
tity of the delimiting character is lost. This function cannot be used on con<AD>
stant strings.
---
ПП
Така бачка:
---
BUGS
Never use this function. This function modifies its first argument. The iden<AD>
tity of the delimiting character is lost. This function cannot be used on con<AD>
stant strings.
---
ПП
Така бачка:
void test(char *buf)
{
char *s = buf;
strtok(s, " ");
printf("%s", s);
return;
}
...
char* str = "ala bala nitza...";
char* buf = (char *)malloc(strlen(str) + 1);
strcpy(buf, str);
test(buf);
free(buf);
Редактирано от mn_t на
Re: segmentation fault
Но не както трябва:
test(buf);
printf("%s", buf); //Изкарва само "ala"
free(buf);
По-добре използвай strsep.
test(buf);
printf("%s", buf); //Изкарва само "ala"
free(buf);
По-добре използвай strsep.
strsep
man strsep:
...
BUGS
This function suffers from the same problems as strtok(). In particular, it modifies the original string. Avoid it.
....
Язък
...
BUGS
This function suffers from the same problems as strtok(). In particular, it modifies the original string. Avoid it.
....
Язък
Re: strsep
xaxaxa ami i az stignah do tuka ama po trudniqt na4in:) radost za o4ite mi... i strstr ima nqkakav podoben problem:)
P.S. za6to da e prosto kato moje da e slojno...
P.S. za6to da e prosto kato moje da e slojno...
Това пък защо
strtok и strsep имат общ проблем - трябва да могат да се викат последователно много пъти и значи някъде трябва да се записва докъде са стигнали. Не че нещо пречи това място да е част от интерфейса и да се изисква от потребителя да дава този storage, ама хората са взели решение да пипат стринга. Лошо няма (има де, ясно), ама пък на strstr защо му е да променя входния низ?
Re: strsep
Хъх.... явно man страницата ми е стара
SYNOPSIS
#include <string.h>
char *strsep(char **stringp, const char *delim);
DESCRIPTION
The strsep() function returns the next token from the string stringp which is
delimited by delim. The token is terminated with a `\0' character and stringp
is updated to point past the token.
RETURN VALUE
The strsep() function returns a pointer to the token, or NULL if delim is not
found in stringp.
NOTES
The strsep() function was introduced as a replacement for strtok(), since the
latter cannot handle empty fields. (However, strtok() conforms to ANSI-C and
hence is more portable.)
CONFORMING TO
BSD 4.4
SEE ALSO
index(3), memchr(3), rindex(3), strchr(3), strpbrk(3), strspn(3), strstr(3),
strtok(3)
GNU April 12, 1993
SYNOPSIS
#include <string.h>
char *strsep(char **stringp, const char *delim);
DESCRIPTION
The strsep() function returns the next token from the string stringp which is
delimited by delim. The token is terminated with a `\0' character and stringp
is updated to point past the token.
RETURN VALUE
The strsep() function returns a pointer to the token, or NULL if delim is not
found in stringp.
NOTES
The strsep() function was introduced as a replacement for strtok(), since the
latter cannot handle empty fields. (However, strtok() conforms to ANSI-C and
hence is more portable.)
CONFORMING TO
BSD 4.4
SEE ALSO
index(3), memchr(3), rindex(3), strchr(3), strpbrk(3), strspn(3), strstr(3),
strtok(3)
GNU April 12, 1993
Re: strsep
STRSEP(3) Linux Programmer's Manual STRSEP(3)
NAME
strsep - extract token from string
SYNOPSIS
#include <string.h>
char *strsep(char **stringp, const char *delim);
DESCRIPTION
If *stringp is NULL, the strsep() function returns NULL
and does nothing else. Otherwise, this function finds the
first token in the string *stringp, where tokens are
delimited by symbols in the string delim. This token is
terminated with a `\0' character (by overwriting the
delimiter) and *stringp is updated to point past the
token. In case no delimiter was found, the token is taken
to be the entire string *stringp, and *stringp is made
NULL.
RETURN VALUE
The strsep() function returns a pointer to the token, that
is, it returns the original value of *stringp.
NOTES
The strsep() function was introduced as a replacement for
strtok(), since the latter cannot handle empty fields.
However, strtok() conforms to ANSI-C and hence is more
portable.
BUGS
This function suffers from the same problems as strtok().
In particular, it modifies the original string. Avoid it.
CONFORMING TO
BSD 4.4
SEE ALSO
index(3), memchr(3), rindex(3), strchr(3), strpbrk(3),
strspn(3), strstr(3), strtok(3)
GNU 1993-04-12 STRSEP(3)
NAME
strsep - extract token from string
SYNOPSIS
#include <string.h>
char *strsep(char **stringp, const char *delim);
DESCRIPTION
If *stringp is NULL, the strsep() function returns NULL
and does nothing else. Otherwise, this function finds the
first token in the string *stringp, where tokens are
delimited by symbols in the string delim. This token is
terminated with a `\0' character (by overwriting the
delimiter) and *stringp is updated to point past the
token. In case no delimiter was found, the token is taken
to be the entire string *stringp, and *stringp is made
NULL.
RETURN VALUE
The strsep() function returns a pointer to the token, that
is, it returns the original value of *stringp.
NOTES
The strsep() function was introduced as a replacement for
strtok(), since the latter cannot handle empty fields.
However, strtok() conforms to ANSI-C and hence is more
portable.
BUGS
This function suffers from the same problems as strtok().
In particular, it modifies the original string. Avoid it.
CONFORMING TO
BSD 4.4
SEE ALSO
index(3), memchr(3), rindex(3), strchr(3), strpbrk(3),
strspn(3), strstr(3), strtok(3)
GNU 1993-04-12 STRSEP(3)
Не,
просто лош дизайн. Най-трудното в програмирането. Нещото, в което Линус е гениален, всъщност
. Очевидно posix не е негов продукт 