Hello!
I just completed writing a program to ask a user for 2 words, pass them into a function, and use strcmp to let the user know which word is larger (or if they are equal). The program compiled and runs, but at the end it tells me 'Segmentation fault (core dumped)'. What does this mean? The only thing I could think of causing it is the 'char *msg = NULL' and printf using it, as this was something the professor told us to include, but never once in the class or the notes explains why? That's the only thing I can think of. Anyway, here is the code:
Any advice would be great! :) Thank you!Code:
#include <stdio.h>
#include <string.h>
void word_order(char *string1, char *string2); /* Create prototype of new function */
int main()
{
char string1[30];
char string2[30];
/* create rest of code here */
printf("\nPlease enter a word no more than 30 characters: ");
scanf("%s", string1);
printf("\nPlease enter another word no more than 30 characters: ");
scanf("%s", string2);
word_order(string1, string2);
return 0;
}
void word_order(char *string1, char *string2) /* New function here. */
{
char *msg = NULL;
/* create rest of code here */
if (strcmp(string1, string2) > 0)
{ printf("First word is bigger than the second\n");}
else if (strcmp(string1, string2) < 0)
{ printf("Second word is bigger than the first\n");}
else
{printf("Word one is equal to word two\n");}
printf("%s\n", msg);
}