User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 425,909 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,810 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 722 | Replies: 13
Reply
Join Date: Jul 2008
Posts: 23
Reputation: coveredinflies is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Newbie Poster

Re: Converting from a string to a character array.

  #11  
Jul 24th, 2008
Originally Posted by joshmo View Post
What didnt you understand?


Erm, I am a bit confused about this bit (hope I have quoted the code properly)

CPP Syntax (Toggle Plain Text)
string str = "Hello";char *cstr = &str[0];string str = "Hello";
char *cstr = &str[0];
Also this way the pointer isn“t const unlike the return value from the string::c_str() function.
(The only problem with this is that it is not a seperate char array, anything you change in cstr will also change in str).



I can remember reading something about only needing the first element of a character array when using pointers, or something similar and I think that is important here but I am more confused about char vs char* in general. I don't understand why it is necessary to use pointers here since to my untrained eye strings and arrays seem to be almost equivalent apart from the [] and the null pointer.


Also if someone could give me a quick explanation of that std:"something here" thing. I see it all over and it seems to be pretty fundamental but I haven't come across it any of the things I was taught or the beginners information I have read. From what I can see (probably wrong) it seems to be like an alternative to a header file. But isn't a header file more convenient? You only have to type it once and the code is much easier to follow (imo).

Sorry for jabbering on a bit. Everyone here seems very helpful
Reply With Quote  
Join Date: Oct 2007
Posts: 232
Reputation: joshmo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: Converting from a string to a character array.

  #12  
Jul 24th, 2008
Everyone has a different way of writing their codes. What you may find long may be easy for someone else. I dont think from what you originally wanted pointers would be of alot of help but it can be done to.

>>Also if someone could give me a quick explanation of that std:"something here" thing

This can be used if the statement "using namespace std" has not been included in the code. All the files in the C++ standard library declare all of its entities within the std namespace. That is why someone can do something like this
 std::cout<<"Hello"; 
Last edited by joshmo : Jul 24th, 2008 at 1:43 pm.
Reply With Quote  
Join Date: Jul 2008
Posts: 23
Reputation: coveredinflies is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Newbie Poster

Re: Converting from a string to a character array.

  #13  
Jul 24th, 2008
Thanks joshmo.
Reply With Quote  
Join Date: Jan 2008
Location: USA East Cost
Posts: 389
Reputation: CoolGamer48 is on a distinguished road 
Rep Power: 1
Solved Threads: 37
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Whiz

Re: Converting from a string to a character array.

  #14  
Jul 24th, 2008
pointers and arrays and similar

link
scroll down around half-way to the section called "Pointers and arrays"
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:58 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC