C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 February 22nd, 2003, 11:12 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
* when initializing constand char?

I don't understand what the * does in:

const char *whatever = "Hello this is a whatever program";

I took it out and the program won't run, I was just wondering what the hell that is needed for. It gives no description in my book, it isn't even in the god damn declaring constants section!
Regards.
-andy
__________________
hmmm...

Reply With Quote
  #2  
Old February 23rd, 2003, 02:17 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
Try looking in the pointer section. You also may want to get another book to cross reference troublesome problems. I can recommend Ivor Horton's Beginning C++ 6.

The "*" denotes a pointer. You can't assign a string of characters to a variable declared like this:

char a_character;

nor this:

const char whatever;

Both "a_character" and "whatever" being of type char will only hold one character(and you have to use single quotes in the assignment statement). In the case of "whatever", it's declared constant, so you can't try to change its value later. On the other hand, an array of type char can hold a string of characters:

char some_text[]="This is the way to do it.";

and a pointer to type char (char*) can also hold a string of characters:

char* pwords="You can do it this way too.";

With both cases you can output the string using only the name:

cout<<text;
cout<<pwords;

Normally, if you try to print out a pointer like in the last line of code, the output will be an address in memory because pointers store addresses. However, pointers to type char are an exception: cout handles them differently and outputs the whole string.(Note: an array name is actually a pointer too.)

Last edited by 7stud : February 23rd, 2003 at 05:03 AM.

Reply With Quote
  #3  
Old February 23rd, 2003, 02:30 AM
Tsigo Tsigo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Posts: 139 Tsigo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Send a message via ICQ to Tsigo Send a message via AIM to Tsigo
Sounds like a stupid thing for the book to do.

Here's an excellent article on pointers: http://www.codeproject.com/cpp/pointers.asp
__________________
The Bibles: PHP / MySQL
EQdkp @ SourceForge

Reply With Quote
  #4  
Old February 23rd, 2003, 10:19 AM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
that article was great..its a great site to! Thanks! and thanks stud..you also explained it very well.

Reply With Quote
  #5  
Old February 23rd, 2003, 02:08 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
Here's a technical point:

With pwords declared like this:

char* pwords;

it looks like the value in memory that pwords points to is not constant: its a pointer to type char after all not a pointer to type const char. However, a string literal at some point became constant in C++, so you cannot try and change it like this:

pwords = "You can't try and change this string."

*pwords='X';

That will compile, but you will get an error when you run your program. So, my book suggests declaring the pointer like this in the first place to keep you from possibly trying to change its value:

const char* pwords;

That declares pword as a pointer to a const char. Then it's obvious you cannot try and change the value pwords points to, and if you do try and change it accidentally, the compiler will find the error.

Last edited by 7stud : February 23rd, 2003 at 02:11 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > * when initializing constand char?

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap