Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 01-23-2006, 06:31 AM   #1 (permalink)
 
Newb Techie

Join Date: Oct 2005

Posts: 5

handshakeit

Question help in C++

Hi All,

When we define the constant size of string, then it should take input of that particlur size but

it does not do so.
For example:-

char str[5];

The above string should take input of maximum size 5, but it can take input of any size greater

that 5.
Why it is so....???

Thanx:
Devs
handshakeit is offline  
Old 01-23-2006, 07:16 AM   #2 (permalink)
 
Newb Techie

Join Date: Oct 2005

Posts: 13

flyacrossawave

Default

Don't forget that when the size of a char string is declared, one of the spaces in memory is used for the null terminator (/0) at the end of the string. So, in you example, str would hold 4 characters.
flyacrossawave is offline  
Old 01-23-2006, 09:56 AM   #3 (permalink)
 
Super Techie

Join Date: Jun 2005

Posts: 274

mgoldb2

Default Re: help in C++

Quote:
Originally posted by handshakeit

The above string should take input of maximum size 5, but it can take input of any size greater
it will compile and maybe sometime work but it a very bad idear. when you declear char a[5] you are reserving space for 5 chars in memory. when you go over that space you are over riding memory space not reserve for it. this can have extremly un expected results.
mgoldb2 is offline  
 
Closed Thread

Thread Tools
Display Modes

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 On