By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,588 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,588 IT Pros & Developers. It's quick & easy.

When you declare an array of chars and store a string in it, where isthe position of the null character \0? And what happens to the unused memorylocations?

P: n/a
When you declare an array of chars and store a string in it, where is
the position of the null character \0? And what happens to the unused
memory locations?

#include <stdio.h>
int main(void)
{
char gstring2[25] = "dudes";
gstring2[5] = 'a';
printf("%s \n", gstring2);
return 0;
}

The output of main function was

dudesa

How come this code works, and the statement
gstring2[5] = 'a';
doesn't overwrite the null character?
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Gary wrote:
When you declare an array of chars and store a string in it,
where is the position of the null character \0?
Wherever you ask it to be, if you ask there to be one.
And what happens to the unused memory locations?
Uninitialised elements of a struct or array object will
be 'zero initialised'.
#include <stdio.h>
int main(void)
{
char gstring2[25] = "dudes";
Same as...

char gstring2[25] =
{ 'd', 'u', 'd', 'e', 's',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
gstring2[5] = 'a';
printf("%s \n", gstring2);
return 0;
}

The output of main function was

dudesa

How come this code works, and the statement
gstring2[5] = 'a';
doesn't overwrite the null character?
It does overwrite the null character, which was followed by
another one.

You need to be careful though of situations like...

char foo[5] = "dudes";

C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.

--
Peter
Jun 27 '08 #2

P: n/a
Gary wrote:
When you declare an array of chars and store a string in it, where is
the position of the null character \0?
After the last character of string data.
And what happens to the unused
memory locations?
They are set to zero.
#include <stdio.h>
int main(void)
{
char gstring2[25] = "dudes";
gstring2[5] = 'a';
printf("%s \n", gstring2);
return 0;
}

The output of main function was

dudesa
You overwrote the null terminator with a character.
How come this code works,
Because there were extra characters that were set to zero due to the
partial initialization.
and the statement
gstring2[5] = 'a';
doesn't overwrite the null character?
It did, but a new terminator was in place.

If you had this:
char str[] = "dudes";

Then you'd probably be in trouble. The next adjacent byte wouldn't even
belong to your object so overwriting the null terminator would cause
undefined behavior as soon as you tried a string operation.

Brian
Jun 27 '08 #3

P: n/a
On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
You need to be careful though of situations like...

char foo[5] = "dudes";

C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.

yes and you get garbage on the screen:

#include <stdio.h>
int main( void )
{
char oye[2] = "ok";

printf("%s\n", oye);

return 0;
}

============= OUTPUT =============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
okH▀ ┐3.L
/home/arnuld/programs/C $

it *accidentally* terminated because at some random place in memory it
found the NULL ?

--
http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)

Jun 27 '08 #4

P: n/a
rio

"arnuld" <su*****@see.sigs.invalidha scritto nel messaggio
news:pa****************************@see.sigs.inval id...
>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
>You need to be careful though of situations like...

char foo[5] = "dudes";

C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.


yes and you get garbage on the screen:

#include <stdio.h>
int main( void )
{
char oye[2] = "ok";
is it not better oye[4] = "ok"; ?
"ok" is o+k+\0
printf("%s\n", oye);

return 0;
}

============= OUTPUT =============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
okH▀ ┐3.L
here someone can see that "\0" is not copied from "ok" to oye[2]
/home/arnuld/programs/C $

it *accidentally* terminated because at some random place in memory it
found the NULL ?

--
http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)


Jun 27 '08 #5

P: n/a
arnuld wrote:
On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
You need to be careful though of situations like...

char foo[5] = "dudes";

C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.


yes and you get garbage on the screen:
printf("%s\n", oye);
No, you get undefined behavior.


Brian
Jun 27 '08 #6

P: n/a
"rio" <a@b.cwrites:
"arnuld" <su*****@see.sigs.invalidha scritto nel messaggio
news:pa****************************@see.sigs.inval id...
>>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
>>You need to be careful though of situations like...

char foo[5] = "dudes";

C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.

yes and you get garbage on the screen:

#include <stdio.h>

int main( void )
{
char oye[2] = "ok";

is it not better oye[4] = "ok"; ?
"ok" is o+k+\0
Not to illustrate the point, no. Both Peter Nilsson and Arnuld posted
code that has an array that is not a string, and in both cases it was
deliberate.

If you want to fix the problem, it is hard to beat 'char oye[] = "ok";'
since you then don't need a size. If you must have a size, anything
other than 3 will be mildly confusing in this example.
>--
http://lispmachine.wordpress.com/
<snip>

Best to trim your replies a bit more. In particular, remove sig blocks
unless you are commenting on them.

--
Ben.
Jun 27 '08 #7

P: n/a
rio
"arnuld" <su*****@see.sigs.invalidha scritto nel messaggio
news:pa****************************@see.sigs.inval id...
>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
>You need to be careful though of situations like...

char foo[5] = "dudes";

C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.


yes and you get garbage on the screen:

#include <stdio.h>
int main( void )
{
char oye[2] = "ok";
is it not better oye[4] = "ok"; ?
"ok" is o+k+\0
printf("%s\n", oye);

return 0;
}

============= OUTPUT =============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
okH▀ ┐3.L
here someone can see that "\0" is not copied from "ok" to oye[2]
/home/arnuld/programs/C $

it *accidentally* terminated because at some random place in memory it
found the NULL ?

--
http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)

Jun 27 '08 #8

P: n/a
rio
"arnuld" <su*****@see.sigs.invalidha scritto nel messaggio
news:pa****************************@see.sigs.inval id...
>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
>You need to be careful though of situations like...

char foo[5] = "dudes";

C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.


yes and you get garbage on the screen:

#include <stdio.h>
int main( void )
{
char oye[2] = "ok";
is it not better oye[4] = "ok"; ?
"ok" is o+k+\0
printf("%s\n", oye);

return 0;
}

============= OUTPUT =============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
okH▀ ┐3.L
here someone can see that "\0" is not copied from "ok" to oye[2]
/home/arnuld/programs/C $

it *accidentally* terminated because at some random place in memory it
found the NULL ?

--
http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)

Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.