Je continue d'obtenir une erreur de segmentation lorsque j'essaie d'ajouter un caractère au test de chaîne. J'ai essayé plusieurs itérations et je n'arrive pas à comprendre pourquoi j'ai l'erreur. J'ai essayé d'avoir le test mis à \0
. Je n'arrive pas à comprendre comment j'accède aux tests externes.Erreur de segmentation en C lors de la tentative de concaténation d'une chaîne et d'un caractère
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#define _XOPEN_SOURCE
#include <unistd.h>
#include <crypt.h>
//this intializes the argument count and argument vector
int main(int argc, string argv[]) {
//this makes sure it is a string
if (argv[1] != NULL) {
//this makes sure that the user typed 2 arguments,
// the second being the keyword
if (argc != 2) {
printf("no hash inputed\n");
return 1;
}
//main program once the keyword has been verified
int h_len = strlen(argv[1]);
string hash = argv[1];
string test = "A";
int p_len = 0;
printf("H len is %i and P len is %i\n", h_len, p_len);
printf("%s and test: %s\n", hash, test);
//this will iterate through the 1st space and test the charachters
for (int j = 0; j < 4; j++) {
//iterates through the characters that can be used in the password
//for (int i = A; i < z; i++)
//for (char ch = 'A' ; ch <= 'z' ; ch == 'Z' ? ch = 'a' : ++ch)
for (char i = 'A'; i <= 'z'; i++) {
//this skips the ASCII inbetween Z and a
if (i > 'Z' && i < 'a') {
i = 'a';
}
printf("%c", i);
//test[j] = test[j] + (char)i;
test = strncat(test, &i, 1);
printf(" test is %s\n", test);
...
Vous devez vérifier 'argc' _before_ vous avez accès' argv [1] '. –
Vous avez alloué un caractère à tester et vous avez essayé de l'ajouter. – Spaceghost
Le code a '// cela s'assure que l'utilisateur a tapé 2 arguments'. Ceux-ci seront dans 'argv [1]' et 'argv [2]' sauf si vous les mettez entre guillemets, par exemple '' un deux '', quand ils seront passés au même argument. Mais le code a 'if (argc! = 2)' qui teste un seul argument ** utilisateur ** - le premier est le nom de l'exécutable. –