0

What is the meaning of the number inside [ ] in a statement such as char a[10];

https://code.sololearn.com/ctYcJ84tk9M6/?ref=app

23rd Dec 2021, 4:06 PM
A G
5 Réponses
+ 3
To add to a bit to what Alex stated: The number inside the brackets [] indicates the number of indices within an array. In your example char a[10] there are 10 indexed memory locations which will store one character variable per location. Indices start at 0 and go to n-1 in a user friendly manner when we speak of arrays. You can create arrays of other types of variables as well, like int, float, double, structs, etc. (list not all inclusive). The distance from one memory address to the next depends on the 'sizeof' the variable, however the index number still only increase by one. For example char a[6] = {'H','e','l','l','o','\0'} **Memory location is only for reference not exact** Mem location Index Binary Value Char Value 1000 0 01001000 H 1001 1 01100101 e 1002 2 10011010 l 1003 3 10011010 l 1004 4 10011101 o 1005 5 00000000 \0 Now if we did this with: unsigned short int numList[3] we could have: *unsigned short int containing two bytes and only positive values* Mem location Index Binary Value Int Value 1000 0 1111001011101011 62187 1002 1 100100101001 2345 1004 2 0000000000000101 5 The memory location jumps by the sizeof the variable, the index number only increases by one The number of bits used are are determined by the variable size. Hope this helps you understand the number in the brackets.
23rd Dec 2021, 5:27 PM
William Owens
William Owens - avatar
+ 4
That is a array of characters. Like this: ["H","e","l","l","o"] As you can see, that is a array whose values are all of type char
23rd Dec 2021, 4:34 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 3
A G can u share that code? I mean i edited ur code just to get output and I see any strange behaviour maybe I misunderstood somthing? https://code.sololearn.com/cD4cO6YbKw0y/?ref=app
24th Dec 2021, 12:16 PM
Atul
Atul - avatar
+ 1
Length of this array would be 10, (as char[10]), means it can store only 10 character values inside
23rd Dec 2021, 5:15 PM
Rupali Haldiya
Rupali Haldiya - avatar
0
Yes thank you, all.......So, in simple words can I sum up that the number denotes maximum number of characters that can be stored in the variable..... But here is the problem, even after giving the number as 1 I can give a full string input of " My name is Arunjit" and still giving the full sentence as output.
24th Dec 2021, 12:13 PM
A G