【问题标题】:C: 2-Dimensional String Array Segmentation FaultC:二维字符串数组分段错误
【发布时间】:2019-01-28 17:30:13
【问题描述】:

尝试制作一个小程序,将大字符串中的单词分开并将(大字符串的)每个单词存储在字符串数组(即指针)中的字符串(即指针)中;形成一个二维字符串数组。 单词分隔符只是一个空格(ASCII 中的 32);大字符串是:


“娱乐圈充氧均衡液化乱穿马路”

注意:

  • 单词长度均为 10 个字符
  • 字符串总长度为 54 个字符(包括空格)
  • 缓冲区的总大小为 55 个字节(包括“\0”)

还有一点,指针数组中的最后一个指针必须为 0(即 1 个字符:'\0')(这完全是任意的)。


这是程序,没什么特别的,但是……

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

int main(void) {

    // The string that we need to break down into individual words
    char str[] = "Showbizzes Oxygenized Equalizing Liquidized Jaywalking";

    // Allocate memory for 6 char pointers (i.e 6 strings) (5 of which will contain words)
    // the last one will just hold 0 ('\0')
    char **array; array = malloc(sizeof(char *) * 6);

    // i: index for where we are in 'str'
    // r: index for rows of array
    // c: index for columns of array
    int i, r, c;

    // Allocate 10 + 1 bytes for each pointer in the array of pointers (i.e array of strings)
    // +1 for the '\0' character
    for (i = 0; i < 6; i++)
        array[i] = malloc(sizeof(char)*11);

    // Until we reach the end of the big string (i.e until str[i] == '\0');
    for (i = 0, c = 0, r = 0; str[i]; i++) {

        // Word seperator is a whitespace: ' ' (32 in ASCII)
        if (str[i] == ' ') { 

            array[c][r] = '\0';     // cut/end the current word
            r++;                    // go to next row (i.e pointer)
            c = 0;                  // reset index of column/letter in word
        }

        // Copy character from 'str', increment index of column/letter in word
        else { array[c][r] = str[i]; c++; }

    }   

    // cut/end the last word (which is the current word)
    array[c][r] = '\0'; 

    // go to next row (i.e pointer)
    r++; 

    // point it to 0 ('\0')
    array[r] = 0; 



// Print the array of strings in a grid - - - - - - - - - - - - - - 

    printf("       ---------------------------------------\n"); 
    for (r = 0; r < 6; r++) {

        printf("Word %i --> ", r);
        for (c = 0; array[c][r]; c++)
            printf("| %c ", array[c][r]);

        printf("|");printf("\n");
        printf("       ---------------------------------------");
        printf("\n");
    }

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    return 0;
}

.. 出了点问题,我不明白如何解决。


由于某种原因,它复制到第一个字符串(即指针),在字符串数组(即指针)中,大字符串的前 6 个字符,然后在第 7 个它给出 分段错误强>。我已经分配了 6 个指针,每个指针有 11 个字节.. 至少我认为代码是这样做的,所以我真的不知道为什么会这样......

希望有人能提供帮助。

【问题讨论】:

  • array[r] = 0; // point it to 0 ('\0') 似乎不正确。
  • 我只是任意希望指针数组中的最后一个指针为0(即'\0');换句话说,字符串数组中保存 1 个字符的最后一个字符串:'\0'。
  • 所以你的意思是// point it to 0 (NULL) 而不是// point it to 0 ('\0'),不是吗?
  • No 不是 NULL,因为最后一个指针分配了内存(容器/字符串存在),只是字符串的唯一字符是 '\0'(ASCII 中的 0)。
  • array[c][r] 如果输入为 array[r][c] 可能会更好。

标签: c string multidimensional-array segmentation-fault dynamic-arrays


【解决方案1】:

将所有出现的array[c][r] 替换为array[r][c]

第一个维度是行。

下次您可以使用调试器检查:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004007ea in main () at demo.c:37
37  array[c][r] = str[i];

【讨论】:

  • 在 gcc 中使用标志 -g 编译,然后 gdb yourprog 并键入 run
  • 谢谢@KeineLust。现在可以了。所以首先是行,然后是列......而且一直以来我都认为这是相反的:S
猜你喜欢
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 2019-08-03
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多