When you declare an array of chars and store a string in it, where isthe position of the null charac

G

Gary

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?
 
P

Peter Nilsson

Gary said:
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.
 
D

Default User

Gary said:
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
 
D

Default User

arnuld said:
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
 
B

Ben Bacarisse

rio said:
arnuld said:
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.
<snip>

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

arnuld

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 ?
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top