I have been searching through various sources, searched through the posts here, and I cannot figure it out why this function is segfaulting on me.
I mean when I execute, it asks for the message, and right after I enter it, the program crashes. I'm pretty sure it does not go past "while". It's so frustrating to be stuck on something so simple as reading an array of chars...Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 41 void read_message(char[]); int main() { char message[SIZE]; read_message(message); return 0; } void read_message(char array[]) { int ch; int i = 0; printf("Enter your message:\n"); while ( (ch=getchar()) != '\n' ) { array[i] = ch; i++; } array[i] = '\0'; }
Thanks in advance.