The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-09-2007, 01:33 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Icon8 Strings & Character Arrays

Quote:
Originally Posted by Konr View Post
This is beautiful. It's not relevant, but it's something everyone starting out in C should see.
Code:
while(*r++=*s++);
It's beautiful. It's by no means the safest string copy code ever, but it is beautiful.
Hahaha. For those that don't understand what is going on, let me break it up:
while(....); simply refers to the fact that the while loop is empty. Normal while loops are done as while(....) { /* whatever */ }

*r++=*s++ could be better formatted this way:
*(r++) = *(s++)

Assuming r and s are arrays (including character arrays which are strings in C), where the beginning of an array is simply a memory address, that code simply says that whatever data is located the memory address pointed to by s should be copied into r at the memory address that r points to.

At the same time, it increments the memory address of r by sizeof(core_data_type_of_r) and the memory address of s by sizeof(core_data_type_of_s). An easy way to do that without worrying about the data type is to use sizeof(*r) and sizeof(*s) because *r and *s just refers to the first bit of data pointed to by r and s.

Lastly, the * operator with arrays (memory pointers) just "dereferences" the pointer, exposing the data. In simpler terms, *r is the data and r is the memory address, where *r might be 'C' and r might be 0x421C7F, for example.


Why would someone do that? Simply put, that is the fastest way to copy a string in C without the need for extra variables or functions.

Why wouldn't you just use r = s to copy the entire string? It is unsafe because you could overwrite data that was originally in s when you modify r and vice-versa. An example:
Before: r1 r2 r3 r4 0 s1 s2 s3 s4 s5 s6 s7 s8 0
r = s
After: <pointer to s> s1 s2 s3 s4 s5 s6 s7 s8 0

Now that r is merely a pointer to s, if you modify r or s, you also modify the other because they both point to the same memory address.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by rpgfan3233 : 07-09-2007 at 04:56 PM.
Reply With Quote
  #2 (permalink)  
Old 07-09-2007, 01:38 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 426
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
Correct me if I'm wrong here, but C doesn't have a native string object, right? What you're really talking about is copying an array of characters? It's been a few years since I looked at C++, but when we did, I had to create my own string class/obj or use a character array.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 07-09-2007, 01:54 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
C has no native string data type, object, etc. In C, strings are null-terminated arrays of characters, such as:
const char* hexadecimal = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '\0'}; /* the '\0' may be replaced by just a 0 without quotation marks */
or you can also do
const char* hexadecimal = "0123456789ABCDEF";

Either way, that is how C strings are declared.

For C++ however, there exists within the standard library (the Standard Template Library, or STL) a class called "string" belonging to the std namespace:
Code:
#include <string>
// uncomment one of the next two lines to use "string" in your code as the data type
// using std::string;
// using namespace std;

// or just use "std::string" in your code as the data type

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-09-2007, 01:56 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 426
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
That's the code way of saying that what you have is an array of characters in C.

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-09-2007, 03:12 PM
Konr Konr is offline
Novice
Join Date: Jul 2007
Posts: 10
iTrader: (0)
Konr is on a distinguished road
Straying off topic ... I took this post to PM.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Konr : 07-09-2007 at 03:17 PM.
Reply With Quote
The Following User Says Thank You to Konr For This Useful Post:
TeraTask (07-09-2007)
  #6 (permalink)  
Old 07-09-2007, 03:18 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 426
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
lol. This thread has now morphed into a c/c++ thread, eh? Should we split this thread to continue such discussion in the appropriate forum?

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 07-09-2007, 04:01 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
YESS!!! Go Go Go!!

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-09-2007, 04:06 PM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Staff*
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 426
iTrader: (0)
TeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enoughTeraTask will become famous soon enough
Quote:
Originally Posted by Konr View Post
Straying off topic ... I took this post to PM.
I split it off now. Will you restore your post?

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 01:15 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50