I am having a conceptual doubt.
char ch[20]="some string";
I wanted to know how ch is being stored i.e. whether 20 bytes are allocated or just the length of the string assigned to it? Can we access something like ch[18] here?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It creates an array by name
chwhich is20characters long and initalizes it with “some string”.Yes, the array is 20 bytes in size.
Yes We can. And even modify the contents.
Good Read:
What is the difference between char a[] = “string”; and char *p = “string”;
To answer Q in comment:
When you do,
The modification has happend only that you cannot see it.
printfdetermines the end of string by detecting\0. The\0was placed at the end of the string when you initialized it.The rule in C/C++ while declaring an array whenever initializers are given, any uninitialized elements are automatically set to 0. As you see in above the diagram depiction the modified character is placed after whatprintfthinks is end of string and hence you cannot see it in output ofprintf.If you output the string each character by a for-loop by iterating over it, You can see your modification.