+ 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
19 Answers
+ 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
+ 5
Thank you very much ~ swim ~ and codemonkey for yours guidance:)
+ 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?
+ 2
Thanks for all :)
+ 2
On!oN you can find answers in following replies.
Because I already ask this question in this section
see the correctly marked answerâ
+ 2
On!oN you can avoid buffer overflows by setting max width of data
Like scanf("%3s",arr);
//Here we can enter upto 3
+ 2
Okay thank you codemonkey :)
+ 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.
0
codemonkey
is it necessary to make a space or storage for null terminator in char array?
what is the use of null terminator?
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
0
it did not do any errors!! u can complete!!
- 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;
}
- 1
codemonkey
#include <stdio.h>
int main() {
char arr[3];
scanf("%s",arr);
printf("%s",arr);
return 0;
}
- 1
Yogeshwaran that's may buffer overflow đ that's why it shows output while input is more than array size
- 1
codemonkey i uses it too for string but i don't realise the benefits of fgets .