![]() |
|
|
|
| ||||||
|
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. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| Quote:
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. Last edited by rpgfan3233 : 07-09-2007 at 04:56 PM. |
| |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |