Please bear with my code. I'm a beginner in C. The code below builds a Vigenere cipher. The user inputs a key
argument which is used to encrypt a plaintext
message. The code will output the ciphertext
.
The error I receive is as follows. Note that I have not studied pointers yet.
Any help in diagnosing the error would be greatly appreciated!
vigenere.c:47:13: runtime error: store to null pointer of type 'char'
Segmentation fault
The code
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[]){
// check for 2 arguments
if (argc != 2){
printf("missing command-line argument\n");
return 1;
}
// check for character argument
int i,n;
for (i = 0, n = strlen(argv[1]); i < n; i++){
if (!isalpha(argv[1][i])){
printf("non-character argument\n");
return 1;
}
}
// if previous 2 checks are cleared, request 'plaintext' from user
printf("plaintext:");
// declare plaintext, key, and ciphertext
string t = get_string(); // plaintext
string u = argv[1]; // key (argument)
string y = NULL; // ciphertext
// encode plaintext with key -> ciphertext
for (i = 0, n = strlen(t); i < n; i++){
if (tolower(t[i])){
y[i] = (char)((((int)t[i] + (int)tolower(u[i%n])) - 97) % 26) + 97;
} else {
y[i] = (char)((((int)t[i] + (int)tolower(u[i%n])) - 65) % 26) + 65;
}
}
printf("ciphertext: %s\n", y);
}