Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

one more thing : how could i determine the size of the array?? #include int main() { char date[2]; char day[2]; char month[4]; char year[6]; char day_month [6]; printf("Please enter your date: "); gets (day); printf("Enter your month: "); gets (month); printf("Please enter your year: "); gets(year); strcpy( day_month,day ); strcat( day_month,"/" ); strcat( day_month,month); strcpy( date,day_month ); strcat( date,"/" ); strcat( date,year); printf("\nYour date is %s ",date);

OpenStudy (anonymous):

if i change the digit in the size, the output will be different

OpenStudy (anonymous):

I think date and day are too small.

OpenStudy (anonymous):

but i want to make sure that the user enter 2 digit for day, 2 digit for month, and 4 digit only for year

OpenStudy (anonymous):

the date and day arrays won't leave enough space for the NUL terminator ('\0') if you give them only a size of 2,

OpenStudy (anonymous):

ooooo..okay..so i should make the size bigger?

OpenStudy (anonymous):

also, I tthink there's an issue with buffer overflow: in one of the statements you are copying a string of size 6, into a buffer of size 2. strcpy( date,day_month );

OpenStudy (anonymous):

Simply by changing the size of the array, the output shouldn't change. Still, there's really no 'right' size for this, since you're using gets to read the strings. If you make the day array 5 bytes long, I could input "Monday" or "365", and gets would write over the bounds of the array. To solve this, you could use scanf instead of gets to read the strings. Scanf will allow you to specify a width modifier to restrict the number of character being read, like so: scanf("%2s", day); That would read a string from stdin, and only accept a maximum of 2 characters.

OpenStudy (anonymous):

you can also use fgets() http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

OpenStudy (anonymous):

but if i use scanf ("%2s",day), when i enter more than 2 digits, the program will not read month

OpenStudy (anonymous):

http://ideone.com/DYBDp changing the sizes makes it seem to work.

OpenStudy (anonymous):

http://ideone.com/MGD3a trimmed the size of the final buffer.

OpenStudy (anonymous):

And agdgdgdgwngo is correct - the line strcpy( date,day_month ); will copy up to 6 characters from day_month to date, which is only 2 characters long. This will overwrite memory you didn't intend to write to and cause a crash or unpredictable behaviour. You should use strncpy, which allows you to specify the maximum length of the string to be copied - in this case, 2. Additionally, if you have a char array that you want to store a string of 2 characters in, you should make the array 3 bytes in size. C strings are null terminated, meaning the last byte after the text string will have the value 0, which is how C string functions identify the end of a string.

OpenStudy (anonymous):

it all boils down to buffer overflow errors; the combination of gets, strcat, and tiny buffer sizes makes a nasty program

OpenStudy (anonymous):

oh..okay..thanks a lot!!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!