为什么我有分段错误?

时间:2015-02-24 23:53:56

标签: c pointers command-line command-line-arguments

 * Description : This program allows a user to enter a password and and the name of a text file. After the user
 * has entered sufficient information, the program will encode the text and save the overwrite the file with the
 * encoded message.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char make_lower (char ch){//This converts the text of the selected file to all be lowered
int i;//initializing

    if (isalpha(ch)==1) ch=tolower(ch);//if the selected item in the file is an uppercase character, then it converts it to lowercase.
    else;//other-wise do nothing
    return ch;
}

void randomize ( char arr[], int n, int pswd )//Function that randomizes the alphabet to use while encoding
{
    int i;//initialing
    char ch;//initializing
    // Use a different seed value so that we don't get same
    // result each time we run this program
    srand (pswd);//using the seed the user has entered

    // Start from the last element and swap one by one. We don't
    // need to run for the first element that's why i > 0
    for ( i = 0; i < 26; ++i)//for loop the iterates through the alphabet array
    {
        // Pick a random index from 0 to 25
        int random = rand()%26;

        // Swap arr[i] with the element at random index
        char tmp = arr[random];//swapping
        arr[random] = arr[i];//swapping
        arr[i] = tmp;//swapping
    }}

char encode (char ch, char alpha[]){//function that encodes a message using the alphabet that was randomized
int i, tmp;//initializing

    if (isalpha(ch)== 2){//if the selected character is a lowercase then it gets swapped with the "encoded" character.
    int k = ch;
    ch = alpha[k-97];
}
return ch;}


int main(int argc,char *argv[]) {

    char alpha[26] = ("abcdefghijklmnopqrstuvwxyz");//initializing char array
    randomize(alpha, 26, argc);
    int i=0;
    char ch;
    while (argv[i] != EOF){
    ch = argv[i];
    ch = make_lower(ch);
    ch = encode(ch,alpha);
    putchar(ch);
++i;
    }
}

我不知道为什么我一直收到错误:分段错误(核心转储)。 我相信我有一个流氓指针,但我不知道如何解决它。我使用过调试器,似乎无法解决问题。

有人可以帮忙吗?谢谢!

2 个答案:

答案 0 :(得分:4)

问题是你循环argv[]寻找你不会遇到的EOF。因此,只要i> = = argc,你的while循环就会超出范围。

 while (argv[i] != EOF){    // ouch !!! 
   ... // process argv[i]
   ++i;
 }

你必须循环i<argc

 for (i = 0; i<argc; i++) { 
     ... // process argv[i]
 } 

其他问题:

作为一般惯例,定义像这样的常量字符串;

char alpha[] = "abcdefghijklmnopqrstuvwxyz"; //will automatically get the right length

但是,您使用的错误长度(忘记字符串的nul-terminator)不是核心转储的原因,因为在randomize()中您只能使用变量来访问此char数组严格小于26,你似乎不会使用依赖于此终结符的c字符串函数。

如果可以确保仅对小写字符进行编码,则函数encode()也只能访问索引0到25。不幸的是,这里存在核心转储的风险,因为isalpha(ch)==2没有确保这一点:isalpha()为字母数字返回非零值。因此,您最好使用islower()代替:

if (islower(ch)) { //if the selected character is a lowercase then it gets swapped with the "encoded" character.
    int k = ch;
    ch = alpha[k-97];  // that's fine because if islower() is true k is between 97 and 122 and thus k-97 is between 0 and 25.  
}

还有其他小问题:

类似的问题出现在make_lower(),您应该使用isupper()。但是这里没有使用索引,所以没有核心转储。

最后,我想知道ch = argv[i];是否编译,因为argv [i]是指向char的指针。所以你应该写ch = *argv[i];

答案 1 :(得分:2)

你的alpha数组有27个元素:26个(每个字母)+&#39; \ 0&#39; (字符串终止符的结尾)

char alpha[27] = "abcdefghijklmnopqrstuvwxyz"

你应该在那之上遍历argc ......

for (int i=0;i<argc;++i) {
   f(argv[i]) ....
}

注意:你的整个程序都不太清楚: - )

相关问题