Why this code shows warning? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

Why this code shows warning?

This code keep on showing warning. Please tell me why this code shows warning and how to avoid this warning. https://code.sololearn.com/cXYlmkQAhL0f/?ref=app

30th Jul 2020, 2:57 PM
Yogeshwaran P
Yogeshwaran P - avatar
15 Respuestas
+ 5
Thanks ~ swim ~ now I understood my mistake. But I have a doubt in char arr[2]; we can store max three data.isn't? Eg char arr[2] = {h,j,k} I don't know am I right Please clarify this
30th Jul 2020, 3:15 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 5
Thank you very much ~ swim ~ and codemonkey for yours guidance:)
30th Jul 2020, 4:16 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
~ swim ~ you mean For char arr[2]; valid indices are 0 and 1 And For char arr[3]; Valid indices are 0,1 and 2 These valid indices only used for storage Am I right?
30th Jul 2020, 3:28 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
Thanks for all :)
30th Jul 2020, 3:45 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
On!oN you can find answers in following replies. Because I already ask this question in this section see the correctly marked answer√
31st Jul 2020, 3:59 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
On!oN you can avoid buffer overflows by setting max width of data Like scanf("%3s",arr); //Here we can enter upto 3
31st Jul 2020, 4:18 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
Okay thank you codemonkey :)
1st Aug 2020, 6:04 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
There is an error because you use 3rd position of array while you contain only 3 character in your array which have position as 0,1,2 respectively.
1st Aug 2020, 6:47 AM
Avinash Tripathi
0
codemonkey is it necessary to make a space or storage for null terminator in char array? what is the use of null terminator?
30th Jul 2020, 3:58 PM
Yogeshwaran P
Yogeshwaran P - avatar
0
codemonkey I have a doubt about null terminator Suppose if we allocate a space for null terminator in char array[3]; // 0 and 1index for string characters and 2 nd index for null terminator Then it necessary to scan the null terminator too using scanf function Like this scanf("%3s", array); //includes null terminator also ? If yes means then null terminator also comes under %s string format specifier?? please clarify my doubt
1st Aug 2020, 2:24 AM
Yogeshwaran P
Yogeshwaran P - avatar
0
it did not do any errors!! u can complete!!
1st Aug 2020, 9:24 AM
Sarah
Sarah - avatar
- 1
Hey can anyone tell me if array size is fixed to 3 then it can store maximum 3 input but when i input more then 3 it shows all input without any error edited[in above code] #include <stdio.h> int main() { char arr[3]; scanf("%s",arr); printf("%s",arr); return 0; }
31st Jul 2020, 3:46 PM
Samarendra Singh
Samarendra Singh - avatar
- 1
codemonkey #include <stdio.h> int main() { char arr[3]; scanf("%s",arr); printf("%s",arr); return 0; }
31st Jul 2020, 3:58 PM
Samarendra Singh
Samarendra Singh - avatar
- 1
Yogeshwaran that's may buffer overflow 🙄 that's why it shows output while input is more than array size
31st Jul 2020, 4:04 PM
Samarendra Singh
Samarendra Singh - avatar
- 1
codemonkey i uses it too for string but i don't realise the benefits of fgets .
31st Jul 2020, 4:38 PM
Samarendra Singh
Samarendra Singh - avatar