Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 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?

Reply
Thread Tools

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?

 
 
Gary
Guest
Posts: n/a
 
      05-05-2008
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?
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      05-05-2008
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
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      05-05-2008
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
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-06-2008
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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-06-2008
"rio" <(E-Mail Removed)> writes:
> "arnuld" <(E-Mail Removed)> ha scritto nel messaggio
> news(E-Mail Removed) 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.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      05-07-2008
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
Your true traveler finds boredom rather agreeable than painful. It isthe symbol of his liberty -- his excessive freedom. He accepts his boredom,when it comes, not merely philosophically, but almost with pleasure. senthilind@gmail.com Computer Support 0 03-02-2008 08:23 AM
What happens when type conversion between signed and unsigned happens? NM C++ 6 09-20-2006 05:39 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
Find a character in a string, at a specified position. How do you do this? Any help will be appreciated.... Looks Like Trent C++ 2 12-05-2004 04:08 AM



Advertisments