The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
* when initializing constand char?
Discuss * when initializing constand char? in the C Programming forum on Dev Shed. * when initializing constand char? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 22nd, 2003, 11:12 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
* 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...
|

February 23rd, 2003, 02:17 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

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.
|

February 23rd, 2003, 02:30 AM
|
|
Contributing User
|
|
Join Date: Aug 2000
Posts: 139
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|

February 23rd, 2003, 10:19 AM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
that article was great..its a great site to! Thanks! and thanks stud..you also explained it very well.
|

February 23rd, 2003, 02:08 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|