Coding a check for array overflow? or how to have no input limit.
Hi guys,
I'm about 4 weeks in to a beginners C course I'm taking online, the information is being given but it seems they never really touch more than the surface.
currently they are saying getting a string is an issue because of array size, and overflowing this causes segmentation faults.
they havent said how to get around this so I've been trying to figure it out as per my code below :
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
//array with 20 char spaces
char s[20];
do
// take user input including white space
{
scanf("%[^\n]",s);
}
// while s is empty
while ( s == NULL);
// convert s to an int so I can check its not > array size
int count = atoi(s);
if (count > 20)
{
// return erro code
return 1;
}
else
{
// print input to screen
printf("%s\n",s);
}
return 0;
}
unsure as to why this isnt working, should I be using a linked list for an input string? we have only just started to look at linked lists.
please help, thanks.