简体   繁体   English

尝试通过指针数组从二维数组打印字符串时出现分段错误

[英]Segmentation fault when trying to print strings from 2D array via array of pointers

Edit:编辑:

So, for anyone having the same problem, it seems that despite initializing the array of pointers to NULL, the array still had some trash written in it.因此,对于遇到相同问题的任何人,似乎尽管将指针数组初始化为 NULL,但该数组中仍然写入了一些垃圾。 I fixed the segmentation by replacing the header of the last while statement(in function printer) with this:我通过将最后一个 while 语句(在函数打印机中)的标题替换为以下内容来修复分段:

while( (wordPtr[ctr] != NULL) && (ctr < MAX_WORDS) )

Original question:原问题:

The first function of the program is supposed to get some text and populate a two-dimensional array of strings with the words in the text, but each word has to appear only once.该程序的第一个函数应该是获取一些文本并用文本中的单词填充一个二维字符串数组,但每个单词只能出现一次。 Then, it has to populate a two-dimensional array of pointers to strings with the addresses of the words in such a way that the second function can then use those pointers to reprint the text in the order it was originally.然后,它必须用单词的地址填充指向字符串的二维指针数组,这样第二个函数就可以使用这些指针按照原来的顺序重新打印文本。

The problem I am facing is that, while the array of strings seems to populate as it should, I face a segmentation fault error whenever I try to print the text using the array of pointers.我面临的问题是,虽然字符串数组似乎按应有的方式填充,但每当我尝试使用指针数组打印文本时,我都会遇到分段错误。

I think that the problem lies somewhere in the lines where I direct the pointer to the word, but other than that I can't find much.我认为问题出在我将指针指向单词的行中的某处,但除此之外我找不到太多。

I tried using gdb, but the result pointed to strlen.s, which is a function that I haven't used anywhere in the code.我尝试使用 gdb,但结果指向 strlen.s,这是我在代码中任何地方都没有使用过的函数。

Thanks in advance.提前致谢。

The code:代码:


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


#define MAX_WORDS 6
#define MAX_WORD_LEN 10

void placer(char wordsArray[MAX_WORDS][MAX_WORD_LEN], char *ptr[MAX_WORDS][2]);
void printer(char *wordPtr[][2], int linelen);

int main (int argc, char *argv[]){
     char wordArray[MAX_WORDS][MAX_WORD_LEN] = {{ '\0' }};
     char *wordPtr[MAX_WORDS][2] = {{ NULL }};
     int linelen = 100;
     
     placer(wordArray, wordPtr); 
     printer(wordPtr, linelen);
     
     return 0;
}

void placer(char wordsArray[][MAX_WORD_LEN], char *ptr[][2]){
    
    int i, j, k, temp, w = 0, g = 0;
    char check[MAX_WORD_LEN] = {'\0'};
    for (i = 0; i < MAX_WORDS; i++) {
        
        scanf("%s", wordsArray[i]);
        
        //Converts to lowercase
        for (j = 0; j < MAX_WORD_LEN; j++) {
            if(isupper(wordsArray[i][j])) {
                wordsArray[i][j] = tolower(wordsArray[i][j]);
            }
        }
        
        //assigns to pointer array
        ptr[w][0] = &wordsArray[i][0];
        
        //Checks if word already exists in array
        for(k = 0; k < i; k++) {
            if(strcmp(wordsArray[i], wordsArray[k]) == 0) {
                //Directs pointer to the already existing instance of the word
                ptr[w][0] = &wordsArray[k][0];
                //replaces the word with \0
                for (g = 0; g < MAX_WORD_LEN; g++) {
                    wordsArray[i][g] = '\0';
                }
                i--;
                break;
            }
        }
        w++;
    }
    
    //prints from array of unique words
    printf("%s\n\n\n", &wordsArray[1][0]);
    for(temp = 0; temp < MAX_WORDS; temp++) {
        printf("%s\n", wordsArray[temp]);
    }
    
    printf("\n##\n");
}

void printer(char *wordPtr[][2], int linelen) {
    
    int ctr = 0, t;
        
    //prints using pointers
    while (wordPtr[ctr] != NULL) {
        printf("%s", *wordPtr[ctr]);
        ctr++;
    }
}

This is an excellent opportunity to learn to use a debugger .这是学习使用调试器的绝好机会。 Your system almost surely provides a way to run the code in a way so you can single-step through the source code, view the value of variables at any point, place breakpoints, etc.您的系统几乎肯定会提供一种以某种方式运行代码的方法,以便您可以单步执行源代码、查看任意点的变量值、放置断点等。

By doing so you can check what the program actually does versus what you expected it to do.通过这样做,您可以检查程序实际执行的操作与您期望它执行的操作。 Most problems of this type are very easily diagnosed by single stepping.这种类型的大多数问题很容易通过单步执行来诊断。 Each debugger has (at least) a small learning curve but it is well worth the effort to learn the basics.每个调试器(至少)都有一个小的学习曲线,但是学习基础知识是非常值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM