[英]I am trying to implent my own strpbrk function in C
The function is supposed to work similarly to the strpbrk
function in C. function 应该与 C 中的
strpbrk
function 类似地工作。 When I run the code, I get a segmentation fault.当我运行代码时,出现分段错误。 I did a rubber duck debug yet couldn't figure out any problem with the code.
我做了一个橡皮鸭调试,但无法找出代码的任何问题。 I am using a gcc compiler on WSL .
我在 WSL 上使用 gcc 编译器。 The main.h header file contains the function declaration
char *_strpbrk(char *s, char *accept)
. main.h header 文件包含 function 声明
char *_strpbrk(char *s, char *accept)
。 Please what am I missing?请问我错过了什么?
#include "main.h"
/**
* _strpbrk - searches the string s for any of a set of bytes
*
* @s: String to be searched
* @accept: Substring of bytes to search for
* Return: Return a pointer to the byte in s that matches one of the bytes in
* accept, or NULL if no such byte is found
*/
char *_strpbrk(char *s, char *accept)
{
int i, j, check = 0, position = 0;
i = 0;
while (*(s + i) != '\0')
{
for (j = 0; *(accept + j) != '\0'; j++) /* Check if character of s in focus is a character in accept. Break out of the for loop if true*/
{
if (*(s + i) == *(accept + j))
{
check = 1;
position = i;
break;
}
}
if (check == 1) /* check if the character of s in focus was found to be in accept. Break out of the while loop if true */
{
break;
}
i++;
}
if (position == 0) /* Check for return value. Return null if whole string traversed*/
{
return ('\0');
}
else
{
return (s + position);
}
}
Forgot to mention that the gcc
flags -Wall -pedantic -Werror -Wextra -std=gnu89
are used during compilation.忘了提到
gcc
标志-Wall -pedantic -Werror -Wextra -std=gnu89
在编译期间使用。
_
as a prefix._
作为前缀。char *mystrchr(const char *str, const char c)
{
while(*str)
{
if(*str == c) return (char *)str;
str++;
}
return NULL;
}
char *mystrbrk(const char *str, const char *brk)
{
while(*str)
{
if(mystrchr(brk, *str)) return (char *)str;
str++;
}
return NULL;
}
Now your code:现在你的代码:
return ('\0');
This is not returning pointer.这不是返回指针。 You are lucky as
'\0'
will convert to pointer NULL.你很幸运,因为
'\0'
将转换为指针 NULL。 Basically, it is very hard to analyze as you overthink many things.基本上,当你想太多事情时,很难分析。
#include "main.h"
/**
* _strpbrk - searches the string s for any of a set of bytes
*
* @s: String to be searched
* @accept: Substring of bytes to search for
* Return: Return a pointer to the byte in s that matches one of the bytes in
* accept, or NULL if no such byte is found
*/
char *_strpbrk(char *s, char *accept)
{
int i, j, check = 0, position = 0;
i = 0;
while (*(s + i) != '\0')
{
for (j = 0; *(accept + j) != '\0'; j++) /* Check if character of s in focus is a character in accept. Break out of the for loop if true*/
{
if (*(s + i) == *(accept + j))
{
check = 1;
position = i;
break;
}
}
if (check == 1) /* check if the character of s in focus was found to be in accept. Break out of the while loop if true */
{
break;
}
i++;
}
if (position == 0) /* Check for return value. Return null if whole string traversed*/
{
return ('\0');
}
else
{
return (s + position);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.