| Programming > C > Re: When you de... |
|
| << Topic |
< Post |
Post 7 of 9 Topic 26076 of 26977
|
Post > |
Topic >> |
Re: 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?
by "rio" <a@[EMAIL PROTECTED]
>
May 6, 2008 at 07:31 PM
|
"arnuld" <sunrise@[EMAIL PROTECTED]
> ha scritto nel messaggio
news:pan.2008.05.07.03.11.12.336850@[EMAIL PROTECTED]
>> 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 @[EMAIL PROTECTED]
the above blog.
> just check the "About Myself" page :)
>


|
9 Posts in Topic:
|
Gary <fordgwf@[EMAIL P |
2008-05-05 15:04:29 |
|
Peter Nilsson <airia@[ |
2008-05-05 15:17:44 |
|
arnuld <sunrise@[EMAIL |
2008-05-07 08:11:13 |
|
"rio" <a@[EM |
2008-05-06 18:34:59 |
|
"Default User" |
2008-05-06 16:59:49 |
|
"rio" <a@[EM |
2008-05-06 19:31:27 |
|
"rio" <a@[EM |
2008-05-06 19:31:55 |
|
"Default User" |
2008-05-05 22:51:22 |
|
Ben Bacarisse <ben.use |
2008-05-06 18:27:35 |
|
Post A Reply:

|