谁能帮助我理解这个strchr()C Segmentation故障?

时间:2021-06-17 22:43:39

I'm diving into pointers and strings in C and I'm still getting used to some concepts. I tried to implement a version of the strchr() function – the same as in string.h – for study purposes, but something basic is still not right.

我正在深入研究C中的指针和字符串,我仍然习惯于一些概念。我试图实现strchr()函数的一个版本 - 与string.h中的相同 - 用于研究目的,但基本的东西仍然不正确。

Here's my code:

这是我的代码:

#include <stdio.h>

char* my_strchr(const char* str, int c){
  if (str == NULL){
    printf("STR is NULL. Finishing the program\n");
    return NULL;
  }
  while (*str != '\0'){
    if (*str == c){
      return (char*) str;
    }
    str++;
  }
  return NULL;
}

int main(){
  char *a = "Hello World!";
  char *b;
  char c;

  printf("Type the character you want to find in the Hello World! string:\n");
  scanf(" %c", &c);

  b = my_strchr(a, c);

  printf("Character found! %c\n", *b);

  return 0;
}

I'm trying to figure out why this is returning a segmentation error. When I use gbd, it tells me that the error is in the last printf, which tries to print the *b.

我试图弄清楚为什么这会返回分段错误。当我使用gbd时,它告诉我错误是在最后一个printf中,它试图打印* b。

Once my_strchr() returns a (char*) str, I'd have to store this return value in a char pointer variable, right?

一旦my_strchr()返回一个(char *)str,我就必须将这个返回值存储在一个char指针变量中,对吗?

2 个解决方案

#1


5  

When my_strchr doesn't find the character in the string, it returns NULL.

当my_strchr在字符串中找不到该字符时,它返回NULL。

In this case b is NULL so *b is undefined behavior, which explains the segfault.

在这种情况下,b为NULL,因此* b是未定义的行为,这解释了段错误。

You might want to check the result of my_strchr before printing *b, e.g.:

你可能想在打印* b之前检查my_strchr的结果,例如:

if (b != NULL) {
  printf("Character found! %c\n", *b);
} else {
  printf("Not found...\n");
}

#2


0  

There are some logic issue like tuple_cat said.

有像tuple_cat这样的逻辑问题。

But I also think you don't understand some concept, your code is not clean from my point of view.

但我也认为你不懂一些概念,从我的角度来看你的代码并不干净。

I guess you just started coding in c so keep coding :)

我猜你刚开始用c编码所以继续编码:)

First you return a char* in your function but you define argument of the function as

首先,在函数中返回char *,但是将函数的参数定义为

char* my_strchr(const char* str, int c)

In standard C you can't touch a constant you can't modify it that's the point of declaring a constant.

在标准C中,你无法触及常量,你不能修改它,这是声明常量的点。

so change the function to

所以将功能更改为

char* my_strchr(char* str, int c)

Then the correct way to return a char from a string is not

然后从字符串返回char的正确方法不是

return (char*)str;

but just

return str;

At the end of your function.

在你的功能结束时。

This way you will send the address of the first char in char* (string). In a char* you do that by just giving the variable name.

这样,您将以char *(字符串)的形式发送第一个char的地址。在char *中,你只需给出变量名即可。

I encourage you to read: https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html

我鼓励您阅读:https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html

RTFM !!! the char* part at 1.3.4 String Constants

RTFM !!! 1.3.4字符串常量的char *部分

Anyway good luck in your learning.

无论如何,你的学习好运。

#1


5  

When my_strchr doesn't find the character in the string, it returns NULL.

当my_strchr在字符串中找不到该字符时,它返回NULL。

In this case b is NULL so *b is undefined behavior, which explains the segfault.

在这种情况下,b为NULL,因此* b是未定义的行为,这解释了段错误。

You might want to check the result of my_strchr before printing *b, e.g.:

你可能想在打印* b之前检查my_strchr的结果,例如:

if (b != NULL) {
  printf("Character found! %c\n", *b);
} else {
  printf("Not found...\n");
}

#2


0  

There are some logic issue like tuple_cat said.

有像tuple_cat这样的逻辑问题。

But I also think you don't understand some concept, your code is not clean from my point of view.

但我也认为你不懂一些概念,从我的角度来看你的代码并不干净。

I guess you just started coding in c so keep coding :)

我猜你刚开始用c编码所以继续编码:)

First you return a char* in your function but you define argument of the function as

首先,在函数中返回char *,但是将函数的参数定义为

char* my_strchr(const char* str, int c)

In standard C you can't touch a constant you can't modify it that's the point of declaring a constant.

在标准C中,你无法触及常量,你不能修改它,这是声明常量的点。

so change the function to

所以将功能更改为

char* my_strchr(char* str, int c)

Then the correct way to return a char from a string is not

然后从字符串返回char的正确方法不是

return (char*)str;

but just

return str;

At the end of your function.

在你的功能结束时。

This way you will send the address of the first char in char* (string). In a char* you do that by just giving the variable name.

这样,您将以char *(字符串)的形式发送第一个char的地址。在char *中,你只需给出变量名即可。

I encourage you to read: https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html

我鼓励您阅读:https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html

RTFM !!! the char* part at 1.3.4 String Constants

RTFM !!! 1.3.4字符串常量的char *部分

Anyway good luck in your learning.

无论如何,你的学习好运。