C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 9th, 2004, 04:26 AM
icelemontea icelemontea is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 icelemontea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Pointers to char

Hello. I have a problem with pointers to char.

First, I know that the following is valid:
char *a = "I am having a cup of coffee.";

However, the following is invalid, right? I must use strcpy, right?
In addition, can I leave the [] to be blank (i.e. not including any integer)?
char *a = new char[100];
a = "I am having a cup of coffee.";

Is the following acceptable? If no, why not?
char *a = "I am having a cup of coffee.";
cin >> a;

How about this?
char *a = new char[100];
cin >> a;

Reply With Quote
  #2  
Old July 12th, 2004, 11:13 AM
drizzle drizzle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: OREGON
Posts: 24 drizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
However, the following is invalid, right? I must use strcpy, right?
In addition, can I leave the [] to be blank (i.e. not including any integer)?
char *a = new char[100];
a = "I am having a cup of coffee.";


just put it in an array from the get go

char a[100] = "I am having a cup of coffee.";

Reply With Quote
  #3  
Old November 19th, 2004, 10:32 PM
code_geek code_geek is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 9 code_geek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
when you use pointers, you have to do something special to write to the actual value of the variable being pointed to. for instance, this:

int b;
int *a = &b;
a = 0;

does not set b to 0, it sets the pointer a to point to memory location 0.
however, this:

int b;
int *a = &b;
*a = 0;

should set b to 0

someone who understands pointers better than me, feel free to correct me!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Pointers to char


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 1 hosted by Hostway