C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 23rd, 2002, 05:39 PM
InstorN InstorN is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: England
Posts: 5 InstorN User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
C++ Pointers and Strings

Help!

What happens if I have the following code?

PHP Code:
 charastring "Hello";
cin >> astring


If my knowledge of C++ pointers, and how they work are correct, that code assigns the address of the first character of "Hello" to the pointer. Then, the next line, takes a string from the user, and assigns the address of the first character of that string to the pointer?

Right?

Reply With Quote
  #2  
Old August 23rd, 2002, 07:01 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
I don't think you can use a char* for cin, can you?

Reply With Quote
  #3  
Old August 24th, 2002, 04:59 AM
InstorN InstorN is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: England
Posts: 5 InstorN User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You can't? Hmm

Reply With Quote
  #4  
Old August 29th, 2002, 05:20 PM
jonsagara's Avatar
jonsagara jonsagara is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: USA
Posts: 286 jonsagara User rank is Private First Class (20 - 50 Reputation Level)jonsagara User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 m 23 sec
Reputation Power: 7
Why don't you use std::string?
__________________
Jon Sagara

"Me fail English? That's unpossible!"

Reply With Quote
  #5  
Old September 16th, 2002, 08:45 PM
wayp wayp is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: South Carolina
Posts: 0 wayp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This may be too much information

This should clear up how to use c strings and c++ strings
Code:
#include <iostream>
#include <string>

int main(){
   char * astring; //how you declare c strings
   astring = new char[strlen("Hello")+1]; // you must set the size plus the null byte '\0'
   strcpy(astring, "Hello"); // how things are copied into a c string
   cout << astring << endl; // Hello
   delete [] astring; // If you new you must delete; however delete comes first
   astring = new char[30]; // Test this by typing in way more then 30 characters
   cin >> astring;
   cout << astring << endl;
}

/*
int main(){
   //With strings, it is much easier b/c you don't worry about memory
   // The trade off is speed; however, it is best to use strings anyway
   // Make sure you #include <string>
   string astring = "Hello";
   cout << astring << endl;
   cin >> astring;
   cout << astring << endl;
   //If you look at the new char[30], you witness a buffer overflow
   // Hackers will type in more characters then allowed to change other parameters
   // This is why I use strings!
}
*/ 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C++ Pointers and Strings


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT