I'm learning C right now, and I copied this little snippet straight from the book I'm using. It segfaults when I run it and I can't figure out why, I ran it through gdb and it stops at line 9 scanf("%s", aName);, but printing the values of the variables brings up nothing suspicious looking. What's wrong with this thing?
#include <stdio.h>
int nameLength(char[]);
main () {
char aName[20] = {'\0'};
printf("\nEnter your first name: ");
scanf('%s', aName);
printf("\nYour first name contains %d letters.", nameLength(aName));
}
int nameLength(char name[]) {
int result = 0;
while (name[result] != '\0') {
result++;
}
return result;
}
edit: I forgot to mention, it didn't even display the prompt or let me enter a name. it crashed immediately after executing it.