SunQuest
           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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 3rd, 2003, 12:52 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
strings vs. constant strings (what's the difference?)

Can someone please explain the difference of strings and constant strings?

(My guess is that constant strings are hardcoded and strings are not. For example, in printf("Hello World!");, "Hello World!" is a constant string. Am I right?)

Reply With Quote
  #2  
Old February 3rd, 2003, 01:07 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
Another thing,

When using the strcpy function, for example, which is defined like this:

- char *strcpy(char *dest, const char *src);

What does "const char *src" mean?

I know that you can pass in what I believe to be a constant string like this:

- strcpy(name,"Matthew");

And I believe you can also pass in a pointer to an array of char (null terminated) like this:

- char *entered_name;
- strcpy(name,entered_name);

..so what does "const char *src" mean altogether?

Last edited by Doucette : February 3rd, 2003 at 01:09 PM.

Reply With Quote
  #3  
Old February 3rd, 2003, 01:40 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
I think the more common term is "string constant" (as opposed to, say, an integer constant), but what's in a name?

Some examples:

char hello[] = "Hello world!";
char *bye = "Goodbye, cruel world!";

In the above, "Goodbye, cruel world!" is a string constant, and is stored in the text segment in the program's object code. That means you can't change it; you'll get a segmentation fault. The actual variable bye on the other hand, is a pointer, and gets 4 bytes (on most machines, anyway) in the data segment, and is initialised to point to the string in the text segment. So you can still change the value of bye, but not the string that it initially points to.

With hello, the string is stored in the data segment, where you can modify it to your heart's content

Finally, about strcpy and the meaning of const in its prototype:

Code:
char *strcpy(char *dest, const char *src);


const is a safeguard; a hint to the compiler, and to the programmer. Basically, when you declare a function's parameter as being constant, you imply that the function will not change the parameter's value.

And with strcpy(), that makes sense. When you make a call to strcpy(), you don't expect it to change the source string's contents. And if you did put code in the function's body that modifies the value, the compiler can issue a warning, or error or whatever.
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #4  
Old February 3rd, 2003, 02:47 PM
Epsilon Epsilon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2001
Location: Long Beach, California
Posts: 86 Epsilon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 10 m 34 sec
Reputation Power: 7
Thanks alot Analyser. Good explination.

Reply With Quote
  #5  
Old February 3rd, 2003, 09:11 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
Beautiful. Thanks.

Reply With Quote
  #6  
Old February 4th, 2003, 04:19 AM
The Dog The Dog is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Cape Town SA
Posts: 3 The Dog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by Analyser
I think the more common term is "string constant"

Or "string literal"

Reply With Quote
  #7  
Old February 4th, 2003, 06:17 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
*snaps fingers*

That's the term I was looking for...

Reply With Quote
  #8  
Old March 7th, 2003, 12:59 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
Declaring and initializing values of variables causes a bit of the confusion. Let me see if I have this right:

Original Code:
-----------------------
char hello[] = "Hello world!";
char *bye = "Goodbye, cruel world!";
-----------------------

1st line: not need to know length in this method. "Hello world!" probably exists in text segment somewhere (or at least in the compiled code) but now it exists in the data segment for sure.
2nd line: creates a pointer. point pointer, bye, to "Goodbye, cruel world!" that exists in text segment


Declaring and Initializing Separated Code:
-----------------------
char hello[13];
strcpy(hello,"Hello world!");
char *bye;
bye = "Goodbye, cruel world!";
-----------------------

1st line: must know length in this method, that's why I put 13 (length of string plus null character).
2nd line: full array copy. "Hello world!", again, probably exists in text segment somewhere (or at least in the compiled code), but now it exists in the data segment for sure.
3rd line: creates a pointer
4th line: point pointer, bye, to "Goodbye, cruel world!" that exists in text segment

Reply With Quote
  #9  
Old March 7th, 2003, 01:00 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
I'll crosslink to another thread that helps clear up the situation:
http://forums.devshed.com/showthrea...&threadid=54168

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > strings vs. constant strings (what's the difference?)


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