Constructs

L

Landrew

There is a new book-ish like entity that is freely available on the web:

"Constructs of the C++ Proramming Language"

The book is intended to serve as a light-weight reference for developers
with modest experience in C++.

Enjoy,
Landrew
 
N

Niels Dekker - no reply address

Landrew said:
"Constructs of the C++ Proramming Language"

To be found at www.landrew.com

Cool! Thanks!


www.landrew.com/cgi-bin/Via/Eval.pl?site=landrew&page=Article&article=01+Strings&path=/Languages/C%2B%2B/Constructs/Strings
says:
void process()
{
char s[100] = "hop on!";
}

The first seven positions in s are set to 'h', 'o', 'p', ' '
(space), 'o', 'n', '!'. The eigth position is set to a special
end of string marker, called the null character. The null
character is denoted by '\0' when it explicitly appears in code.

s[8] to s[99] are set to a null character as well.

Thus the code above has the same effect as the following:

void process()
{
char s[100];

s[0] = 'h';
s[1] = 'o';
s[2] = 'p';
s[3] = ' ';
s[4] = 'o';
s[5] = 'n';
s[6] = '!';
s[7] = '\0'; // end of string marker (null character)
}

This version will leave s[8] to s[99] uninitialized. Also it might take
less memory during runtime, because it won't store this literal string,
"hop on!".


Regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
 
L

Landrew

s[8] to s[99] are set to a null character as well.

interesting. are you certain about that? i would have thought it would be
true for global variables, but not for local variables.

just as this initializes x to zero

----
int x;

int main()
{
...
}
----

but this does not

----
int main()
{
int x;

...
}
----

at least that is what i thought :)

landrew


Niels Dekker - no reply address said:
Landrew said:
"Constructs of the C++ Proramming Language"

To be found at www.landrew.com

Cool! Thanks!


www.landrew.com/cgi-bin/Via/Eval.pl?site=landrew&page=Article&article=01+Strings&path=/Languages/C%2B%2B/Constructs/Strings
says:
void process()
{
char s[100] = "hop on!";
}

The first seven positions in s are set to 'h', 'o', 'p', ' '
(space), 'o', 'n', '!'. The eigth position is set to a special
end of string marker, called the null character. The null
character is denoted by '\0' when it explicitly appears in code.

s[8] to s[99] are set to a null character as well.

Thus the code above has the same effect as the following:

void process()
{
char s[100];

s[0] = 'h';
s[1] = 'o';
s[2] = 'p';
s[3] = ' ';
s[4] = 'o';
s[5] = 'n';
s[6] = '!';
s[7] = '\0'; // end of string marker (null character)
}

This version will leave s[8] to s[99] uninitialized. Also it might take
less memory during runtime, because it won't store this literal string,
"hop on!".


Regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
 
N

Niels Dekker - no reply address

www.landrew.com says:
void process()
{
char s[100] = "hop on!";
}

The first seven positions in s are set to 'h', 'o', 'p', ' '
(space), 'o', 'n', '!'. The eigth position is set to a special
end of string marker, called the null character. The null
character is denoted by '\0' when it explicitly appears in code.

And I commented:
s[8] to s[99] are set to a null character as well.
interesting. are you certain about that?

Well... home.tiscalinet.ch/t_wolf/tw/c/string_init.html says:
yes, if a string literal in an initializer contains less characters
than the array has elements, the remaining elements are set to 0.

I guess the same holds in C++.


Regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top