- #1
MinusTheBear
- 22
- 0
Hey all,
I have a question regarding C-Strings.
Say I have the following function
This will actually print out the string literal.
I'm confused because the book defines a C-String as: a sequence of characters stored in consecutive memory locations and terminated by a null character. And that a C-String can appear in a program as a programmer-defined arrays of character (like my above code).
So I understand that "Hi" would have two subscripts 0 and 1, and at the end of the i it would have a null character \0. But, it'd also have two empty locations since I defined my array to carry 5 characters. However, since this is an array, shouldn't it require a loop? Does the compiler do this implicitly?
Secondly, if a C-String stores the address of the first character of the string literal, why doesn't printing out cstr1 display an address?
My book demonstrates these concepts but doesn't really explain why it's doing this.
Thanks.
I have a question regarding C-Strings.
Say I have the following function
C:
#include <iostream>
using namespace std;
int main() {
const int SIZE = 5;
char cstr1[SIZE] = "Hi";
cout << cstr1;
return 0;
}
This will actually print out the string literal.
I'm confused because the book defines a C-String as: a sequence of characters stored in consecutive memory locations and terminated by a null character. And that a C-String can appear in a program as a programmer-defined arrays of character (like my above code).
So I understand that "Hi" would have two subscripts 0 and 1, and at the end of the i it would have a null character \0. But, it'd also have two empty locations since I defined my array to carry 5 characters. However, since this is an array, shouldn't it require a loop? Does the compiler do this implicitly?
Secondly, if a C-String stores the address of the first character of the string literal, why doesn't printing out cstr1 display an address?
My book demonstrates these concepts but doesn't really explain why it's doing this.
Thanks.