Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: When you de...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 9 Topic 26076 of 26972
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 Ben Bacarisse <ben.usenet@[EMAIL PROTECTED] > May 6, 2008 at 06:27 PM

"rio" <a@[EMAIL PROTECTED]
> writes:
> "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

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.
 




 9 Posts in Topic:
When you declare an array of chars and store a string in it, whe
Gary <fordgwf@[EMAIL P  2008-05-05 15:04:29 
Re: When you declare an array of chars and store a string in it,
Peter Nilsson <airia@[  2008-05-05 15:17:44 
Re: When you declare an array of chars and store a string in it,
arnuld <sunrise@[EMAIL  2008-05-07 08:11:13 
Re: When you declare an array of chars and store a string in it,
"rio" <a@[EM  2008-05-06 18:34:59 
Re: When you declare an array of chars and store a string in it,
"Default User"   2008-05-06 16:59:49 
Re: When you declare an array of chars and store a string in it,
"rio" <a@[EM  2008-05-06 19:31:27 
Re: When you declare an array of chars and store a string in it,
"rio" <a@[EM  2008-05-06 19:31:55 
Re: When you declare an array of chars and store a string in it,
"Default User"   2008-05-05 22:51:22 
Re: When you declare an array of chars and store a string in it,
Ben Bacarisse <ben.use  2008-05-06 18:27:35 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:27:17 CDT 2008.