-1
Good afternoon, folks, I’d like to request a hand on the college ATV;
About this code used as an example:
#include <stdio.h>
#include <string.h>
int main()
{
char str0[6]="98765",str1[5]="abcd",str2[10]="efghijklm";
printf("str0 = %s\t str1 = %s\t str2 = %s\n",str0,str1,str2);
strcpy(str1,"1234567");
printf("str0 = %s\t str2 = %s\n",str0,str2);
}
the following questions were asked:
first) What happens with the string "str0"?
2nd) What would be your suggestion to avoid the problem?
From what I understand on the subject, the first question on the str0 would only suffer from buffer overflow if there were more characters than 6, that’s not it?
and on question 2), the use of the fgets() command would be useful to avoid buffer overflow?
I would really appreciate it if someone could help, I’m two days hammering on this issue :@
Just one detail: you forgot the string terminator character, '0', which occupies a position. So "abc" occupies 4 positions and not 3.
– anonimo
If I understand correctly, is str0 being overwritten in str1 because of the use of strncpy()? Thank you so much for your help, your explanation helped me a lot!
– Carlos Sanches
Maybe so, and in case you meant strcpy()?
– FourZeroFive
Yes, I meant strcpy! Thanks for the help
– Carlos Sanches