![]() |
|
|
|
| ||||||
|
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 | ![]() |
| |||
| Arrays in C++ are passed by reference when used as arguments to functions, just like in C, though it is often done via pointer notation: Code: // the last element of the array must be a null terminator
// ('\0' in character notation or just the integer value 0)
void print (char * string_to_print) {
cout << string_to_print << endl;
} Code: void print (char string_to_print[]) {
cout << string_to_print << endl;
} Last edited by rpgfan3233 : 07-15-2007 at 12:33 AM. Reason: Some extra stuff... |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-15-2007) | ||
| |||
| LOL char is just the data type of the array. As for your function, it would need a name first and a few other things would need to be done. First of all, I had planned on referencing the fact that arrays in C and C++ are not like in Java, where you can simply use <array_name>.length to get the number of elements in the array. In C and C++, you need to use "sizeof(<array_name>) / sizeof(*<array_name>). However, this does not work with dynamic arrays or strings ("malloc"/"calloc"/"realloc" in C, "new <data_type>[num_elements]" in C++). Therefore, when using dynamic arrays, you must keep track of things yourself, usually the length of the array/string is most important. Also, you wouldn't use "char.length" in Java, would you? The same basic tools that apply in Java also apply in C++. However, things like operator precedence are a bit different. As a result, I've learned to put parentheses around certain things like bitwise operations in an if statement: if (1 & 3 == 1) would be false in Java because it evaluates it like this: "if (1 & (3 == 1))" is "if (1 & false)" which is like "if (0)". The solution is to do this: if ((1 & 3) == 1) Now as for your function: Code: void some_function () {
char string_to_print[256];
for (int i = 1; i < (sizeof(string_to_print) / sizeof(*string_to_print)) - 1; i++) {
string_to_print[i & 255] = (i & 255);
}
string_to_print[255] = 0;
} Code: void some_function () { (In C99 (not C++), it is required, I believe, to specify "int main (void)" rather than "int main ()" when there are meant to be no command-line arguments to the program.) Code: char string_to_print[256]; Code: for (int i = 1; i < (sizeof(string_to_print) / sizeof(*string_to_print)) - 1; i++) { Code: string_to_print[i & 255] = (i & 255); Also, there is for a good reason for the bitwise AND there. For one thing, using "i % 256", where 256 is the number of characters in the string, including the null terminator, would be a pain. There is an old trick where if the value that you are modding by is a power of two, you can just use a bitwise AND to do the same thing as long as you subtract 1 from the power of two. For example, to do 9 % 4, you could just say 9 & 3, which in binary is 1001 AND 0011, which is 0001 (1 in decimal). Is that correct? Let's see! 9 / 4 = 2, remainder = 1 (4 * 2 = 8, 8 + 1 = 9). It was used for efficiency in the days of slower computers and I'm not sure how efficient it makes things anymore, but I still use it. Code: string_to_print[255] = 0; The rest is easy to figure out. To summarize:
I hope this helps. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-15-2007) | ||
| |||
| Quote:
If you've ever studied the Binary (Base-2) number system, then you've probably a good idea what AND does when you compare two 'bits' for example, take these numbers in decimal (Base 10), 9 and 7 When you perform a Bitwise AND on these two numbers, the effect of the operation only makes sense when you view their binary representation 9 in binary is 1001 7 in binary is 0111 so 7 AND 9 yields 0001 (which is 1 in decimal) (If you're not too sure on the link between computers and binary, or how to convert decimal to binary, then i'd suggest doing some googling )Note, & is not to be confused with &&, which is the Logical AND operator... the use of which generally involves comparisons. |
| |||
| Quote:
Code: 1 0 0 1 & & & & 0 1 1 1 0 0 0 1 This kind of arithmetic is somewhat analogous to the way you learn basic math at primary school.. ie, isolating each digit, and performing the given operation. eg, how would you add the numbers 140 and 612 together? Code: ( Decimal / Base-10 ) 1 4 0 + + + 6 1 9 = = = 7 5 9 Last edited by Bench : 07-15-2007 at 02:36 PM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (07-15-2007) | ||
| |||
| Quote:
One way to look at it, is that everything in your computer boils down to '1's and '0's ( or 'on'/'off' .. or 'true'/'false' ..etc).. however, different circuits inside the system use the status of a bit (or of a series of bits) for different purposes.. You have some circuits which are designed for arithmetic, where bitwise logic comes into play, and other circuits which are designed for decision-making, where conditional logic is used. Of course, its a bit more complicated than that, but thats the 'gist' of it. Last edited by Bench : 07-15-2007 at 03:44 PM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (07-15-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |