c - 函数中的操作数组 - c - 段错误

标签 c arrays string pointers

所以我几周前开始学习如何编码,这个网站对我帮助很大,谢谢你。但是这次我卡住了,无法真正弄清楚为什么......希望你能帮助我。 基本上我有一个函数原型(prototype),我必须在我的程序中使用它,但我遇到了麻烦。该函数应接收一个字符串,然后仅复制该字符串的每个第二个字符并返回结果...

这是我目前所得到的:

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

#define max_size 1000

char * everySecondChar(char * dest, char * input);

int main() {

    char inputstr[max_size] = {0}; 
    char *input[max_size] = {0};
    char *dest[max_size] = {0};
    char temp[max_size] = {0}; 
    int i = 0;

    while (fgets(inputstr, max_size, stdin) != NULL)
    {
        input[i] = strndup(inputstr, max_size);
        strcat(temp,inputstr);
        i++;

    }   
    input[0] = strndup(temp, max_size);

    printf("Inputted text:\n%s", *input);
    printf("\n");
    printf("\n");
    printf("Resulting string:\n");

    everySecondChar(*dest, *input);

    printf("%s", *dest);

    return 0;   
}

char * everySecondChar(char * dest, char * input)
{
    int i = 0;

    for(i = 0; i < max_size; i+=2) {
        strcat(dest,input);
    }

    return dest;
}

我知道这对你们大多数人来说可能是一个 1 分钟的挑战,但每当我在函数原型(prototype)中看到那些令人讨厌的 * 时,我都会遇到麻烦 :(

最佳答案

恭喜您开始编程!

关于你的问题:有很多事情可以解决,但由于似乎存在一些更基本的混淆和误解,我将根据你的问题的上下文解决有意义的问题。

首先,您使用的是 strcat当您只需要简单的字符分配时,它连接字符串(例如添加到字符串)。

接下来,你有很多指向数组的指针,而且指针似乎有些困惑;在您的 main 函数中,您不需要所有的临时变量来执行您想要的操作。

你可以简单地:

char inputstr[MAX_SIZE] = {0}; 
char dest[MAX_SIZE] = {0};

你可以(现实地)少一些,但我们现在会坚持基本的。

接下来,您将循环获取用户输入:

while (fgets(inputstr, max_size, stdin) != NULL)
{
    input[i] = strndup(inputstr, max_size);
    strcat(temp,inputstr);
    i++;
}

在这里,您不检查i 是否超过了您的input 变量分配给的max_size;如果 i 超过 max_size 当你将 input[i] 分配给 strndup 返回的内存位置时(这调用 malloc),您正在超出内存范围进行写入,这也称为缓冲区溢出。这可能是您的段错误发生的地方。当您执行 strcat(temp,inputstr); 时,您也可能会遇到一些问题,因为 strcat:

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

如果您只是想获取用户输入的内容,并使用您的函数打印每第二个字符,则无需循环:

if (fgets(inputstr, MAX_SIZE, stdin) != NULL) {
    everySecondChar(dest, inputstr);
    printf("Inputted text:\n%s\n\nResulting string:\n%s\n", inputstr, dest);
}

最后,在您的 everySecondChar 函数中,您再次使用 strcat 时您需要做的只是简单的赋值(执行“复制”):

char * everySecondChar(char * dest, char * input)
{
    int i, j;
    for(i = 0, j = 0; i < MAX_SIZE; ++i, ++j) {
        if (input[i] == 0) break; // end if string?
        dest[j] = input[i++];
    }
    return dest;
}

将所有这些放在一起,您会得到:

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

#define MAX_SIZE 1000

char * everySecondChar(char * dest, char * input);

int main(void)
{
    char inputstr[MAX_SIZE] = {0}; 
    char dest[MAX_SIZE] = {0};
    printf("Enter some text: ");
    if (fgets(inputstr, MAX_SIZE, stdin) != NULL) {
        everySecondChar(dest, inputstr);
        printf("Inputted text:\n%s\n\nResulting string:\n%s\n", inputstr, dest);
    }
    return 0;   
}

char * everySecondChar(char * dest, char * input)
{
    int i, j;
    for(i = 0, j = 0; i < MAX_SIZE; ++i, ++j) {
        if (input[i] == 0) break; // end if string?
        dest[j] = input[i++];
    }
    return dest;
}

除此之外,我还要谈谈其他事情;通常如果你有一个常量值,比如你的 max_size 变量,它被认为是“最佳实践”将整个事物大写:

`#define MAX_SIZE 1000`

I am having my troubles whenever I see those nasty * in a function prototype :(

函数原型(prototype)(和变量声明)中那些讨厌的 * 被称为指针限定符;它表示变量的类型是指向指定类型的指针。指针不是什么可怕的东西,您正在学习 C,了解指针是什么及其实用程序非常重要。

我不会深入探讨指针、别名等的所有特殊性,因为这超出了本问答的范围,但是 WikiBooks有很好的介绍和解释,涵盖了很多这些概念。

希望对您有所帮助!

关于c - 函数中的操作数组 - c - 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47168449/

相关文章:

c - 如何在 C 中截断文件?

c - 在 LGPL 代码中包含 GPL header

c++ - 数组分区函数

c# - 我需要从字符串中去除所有符号以创建一个忽略标点符号的 `IEqualityComparer`

C float 小数不被内存

java - java中的零长度数组

java - 具有多于一列的动态数组

python - 使用 pandas 使用列中的值格式化字符串

python - 如何测试一个字符串是否有大写字母

c# - 链接 C#(C-sharp)和 C 语言