سؤال

when i start my program i give a input like this:

EKMFLGDQVZNTOWYHXUSPAIBRCJ 16
AJDKSIRUXBLHWTMCQGZNPYFVOE 4
BDFHJLCPRTXVZNYEIWGAKMUSQO 21
ABCDEFGDIJKGMKMIEBFTCVVJAT
2
MCK
QMJIDOMZWZJFJR
ABC
ESTAMENSAGEMVAISERCIFRADA

The program reads each line and then executes some methods, but when it reaches the fifth line ("2") it crasches with the error message : "No source available for "0xb7e9f84f" " The code in that corresponds to that reading is the following:

fgets(cadeia1, 31, stdin);
int rPos1 = getRotationPos(cadeia1);
fgets(cadeia2, 31, stdin);
int rPos2 = getRotationPos(cadeia2);
fgets(cadeia3, 31, stdin);
int rPos3 = getRotationPos(cadeia3);
fgets(cadeiaRef, 26, stdin);

r1 = createRotor(r1, cadeia1, rPos1);
r2 = createRotor(r2, cadeia2, rPos2);
r3 = createRotor(r3, cadeia3, rPos3);
ref = createReflector(ref, cadeiaRef);
m->r1 = r1;
m->r2 = r2;
m->r3 = r3;
m->ref = ref;

char* messages; 
fgets(messages, 3, stdin);     ////////////////////// This is where it crashes
int nMessages = atoi(messages);

I would like to know whats wrong :s, thanks anyway!

هل كانت مفيدة؟

المحلول 2

You have not allocated space for messages (based on what you have posted). You either need to allocate space (malloc function) or declare a character array before using.

For example:

 char messages[3];
 fgets(messages,3,stdin); 

or

 char *messages;
 /* ask for memory */
 *message = malloc(3);
 fgets(messages,3,stdin);
 /* do some processing */
 /* release the memory back */
 free(messages);

نصائح أخرى

You're passing an uninitialized pointer to fgets change your code to

char messages[256]; // or w/e max length you have dictated.
fgets(...);

You're most likely corrupting memory.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top