0

Problem with segmentation. I can compile the program, but can't run. Questions about segmentation fault were asked many times, but I still don't know. I think problem with command-line argument — I simply don't know how to use it. Eventually all I get — runtime error: load of null pointer of type 'char' (k1 = *argv[1];) Segmentation fault

#include <stdio.h>//printf
#include <stdlib.h>//atoi 
#include <cs50.h> //get_string, get_int
#include <string.h>//strlen
#include <math.h>//fmod, fmodf, fmodl

int main(int argc,char *argv[])
{
    if (argc != 1)
        {
        printf("error: less or more than one command-line arguments\n");
        return 1;
        }
    //Получение открытого текста
    int q,i,j,b,k;
    char k1;
    double a;
    k1 = *argv[1];
    k = atof(&k1);
    string f=get_string("plaintext: ");
    char Alpha[26], lAlpha[26];


    //Получение длины открытого текста
    q=strlen(f);

    for (i=0;i<25;i++)
        Alpha[i]=i+65;

    for (i = 0;i<25;i++)
        lAlpha[i]=i+95;
    //Данный цикл проходит все значения (буквы) открытого текста
    for (i=0;i<q;i++)
        //Данный цикл проходит все значения (буквы) алфавита
        for(j=0;j<26;j++)
        {
            //Сравнение букв открытого текста с буквами алфавита
            if (f[i]==Alpha[j])
            {
            //Операция по нахождению остатка от деления номера буквы
            a=fmod(j+k,26);
            b=a;
            f[i]=Alpha[b];
            break;
            }

            //Сравнение букв открытого текста с буквами алфавита
            if (f[i]==lAlpha[j])
            {
            //Операция по нахождению остатка от деления номера буквы    
            a=fmod(j+k,26);
            b=a;
            f[i]=lAlpha[b];
            break;
            }
        }    
    printf("ciphertext: %s\n",f);
}
0

I see some mistakes

char k1;
k1 = *argv[1];
k = atof(&k1);

char is character like 'A' not a word or string like "hello" if you need string variable, write char *some_string; Function atof() return float, not integer. For int you can use atoi(char *c) in result

    char *k1;
    k1 = argv[1];
    k = atoi(k1);
4
  • now I got new problem. caesar3.c:19:14: runtime error: null pointer passed as argument 1, which is declared to never be null /usr/include/stdlib.h:148:33: note: nonnull attribute specified here Segmentation fault Nov 5 '18 at 12:29
  • and what is on line 19?
    – Pavel
    Nov 6 '18 at 15:55
  • k1 = *argv[1]; Nov 10 '18 at 13:31
  • What I wrote was wrong. Line 19 is k = atoi(k1);. Nov 11 '18 at 13:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .