简体   繁体   English

我正在尝试在 C 中实现我自己的 strpbrk function

[英]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在编译期间使用。

  1. Do not use _ as a prefix.不要使用_作为前缀。
  2. This task shows how useful is use of the functions此任务显示了使用这些功能的有用性
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.

相关问题 strpbrk函数 - C. - strpbrk function - C 我正在尝试创建自己的 printf 函数,但我不知道如何包含格式说明符 - I am trying to create my own printf function, but I dont know how to include the format specifiers C中的数组和strpbrk - Arrays and strpbrk in C 带有strpbrk的c中的字符和字符串 - Characters and Strings in c with strpbrk 我正在尝试使用 C 中的递归来反转链表,我对我的递归 function 有一些疑问 - I am trying to reverse a linked list using recursion in C, I have some doubts on my recursive function 我正在尝试制作自定义 pow() function 但我的 for 循环不起作用 - C - I am trying to make a custom pow() function but my for loops are not working - C 我正在尝试创建一个函数来打开文件以在C中读取 - I am trying to create a function that opens a file for reading in C 如何在C中创建自己的floor函数? - How do I create my own floor function in C? 是否有一个函数可以在 C 中舍入浮点数,还是我需要自己编写? - Is there a function to round a float in C or do I need to write my own? 我正在尝试使用 C 中的邻接矩阵来实现 BFS,但我的代码在第一行之后没有处理出队 function - I am trying to implement BFS using adjacency matrix in C,but my code is not processing the dequeue function after the first row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM