C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Using character pointers Posted by talhanasir on 8 Jun 2006 at 12:25 AM
How come the value of last cout statement is
My sister's married name is name was

char *c = "Bettye Lou Horn";
cout << "My sister's maiden name was" << c << "\n";
c = c+11; // Make c point to the last name (the 12th character)
c = "Henderson";
c = c - 11; // Make c point to its original location.
cout << "My sister's married name is" << c << "\n";
// when we use *c in cout statement only B prints why

Report
Re: Using character pointers Posted by Lundin on 8 Jun 2006 at 1:52 AM
: c = "Henderson";

You can't do this. You will set the pointer to point at a new location - the static string "Henderson". It will forget where it pointed at before.

: c = c - 11; // Make c point to its original location.
So when you subtract 11 here you are pointing at a non-valid memory location. The program will likely crash here.

Try something like this instead:


char str[21] = "Bettye Lou Horn"; 
/* Make str an array of chars large enough to hold "Bettye Lou Henderson" = 20 letters + 1 null termination */

char* lastName = "Henderson";

cout << "My sister's maiden name was " << str << endl; 

strcpy(str+11, lastName);

cout << "My sister's married name is " << str << "\n"; 


Report
Re: Using character pointers Posted by AsmGuru62 on 8 Jun 2006 at 4:29 AM
This message was edited by AsmGuru62 at 2006-6-8 4:29:39

:
: char str[21] = "Bettye Lou Horn"; 
: /* Make str an array of chars large enough to hold "Bettye Lou
 Henderson" = 20 letters + 1 null termination */
: 
: 

:
Just one small addition: if you omit the value in brackets the compiler will allocate memory exactly enough to hold a string, so you do not have to count symbols:
:
: char str[] = "Bettye Lou Horn"; 
: 




Report
Re: Using character pointers Posted by Lundin on 8 Jun 2006 at 4:38 AM
: This message was edited by AsmGuru62 at 2006-6-8 4:29:39

: :
: : char str[21] = "Bettye Lou Horn"; 
: : /* Make str an array of chars large enough to hold "Bettye Lou
:  Henderson" = 20 letters + 1 null termination */
: : 
: : 

: :
: Just one small addition: if you omit the value in brackets the compiler will allocate memory exactly enough to hold a string, so you do not have to count symbols:
: :
: : char str[] = "Bettye Lou Horn"; 
: : 

:
:
:
:

Yep, but in this case it should be initialized to
"Bettye Lou Horn" but be large enough to be able to hold
"Bettye Lou Henderson"
Report
Re: Using character pointers Posted by AsmGuru62 on 8 Jun 2006 at 11:55 PM
: : This message was edited by AsmGuru62 at 2006-6-8 4:29:39

: : :
: : : char str[21] = "Bettye Lou Horn"; 
: : : /* Make str an array of chars large enough to hold "Bettye Lou
: :  Henderson" = 20 letters + 1 null termination */
: : : 
: : : 

: : :
: : Just one small addition: if you omit the value in brackets the compiler will allocate memory exactly enough to hold a string, so you do not have to count symbols:
: : :
: : : char str[] = "Bettye Lou Horn"; 
: : : 

: :
: :
: :
: :
:
: Yep, but in this case it should be initialized to
: "Bettye Lou Horn" but be large enough to be able to hold
: "Bettye Lou Henderson"
:
I see... I stand corrected.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.