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   
  #11 (permalink)  
Old 07-14-2007, 11:57 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Is Array in C++ passed by reference or value?

Edit: or should I wait for your tutorial..?

__________________
PHP Code:
System.out.println("Hello World!"); 

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
  #12 (permalink)  
Old 07-15-2007, 12:26 AM
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
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;
}
You can also do the following:
Code:
void print (char string_to_print[]) {
    cout << string_to_print << endl;
}
For some reason, even though the data type is char[], arrays are one bit of C++ that still prefers things to be associated with the variable rather than with the data type, which the creator of C++ didn't want. In C, the idea is that string_to_print is a pointer to a char in memory or a pointer to an array of chars in memory. In C++, the idea is that string_to_print is a char pointer (like in C) or an array of chars in itself. However, one can still use pointer notation for referencing things in arrays, though there are restrictions, such as you can't change where an array begins (understandable since it will screw up the idea that array_a[42] would still have 42 elements, but the data used by those 42 elements would be shifted).

__________________

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-15-2007 at 12:33 AM. Reason: Some extra stuff...
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-15-2007)
  #13 (permalink)  
Old 07-15-2007, 12:41 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
So, there's only one type of Array in C++ ??? char ??? If that so, then you need to parse everything out before storing them in arrays then...?? *That's d**n difficult* Should create a method for that one first then if it does so...

Can I do this in C++:

Code:
void () {
    char string_to_print[]
    for (int i=0;i<char.length;i++){
         string_to_print[i] = i; // I have no idea what I'm doing this is just Java
    }
}

__________________
PHP Code:
System.out.println("Hello World!"); 

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
  #14 (permalink)  
Old 07-15-2007, 01:30 AM
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
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;
}
A few strange things went on there, right? Let's start with the basics of it:
Code:
void some_function () {
That denotes the beginning of a function named "some_function" with no return value because the return type is "void". There are no arguments to the 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];
What is this one about? Well, that just says to create a char array named "string_to_print" with a maximum capacity of 256 characters.

Code:
for (int i = 1; i < (sizeof(string_to_print) / sizeof(*string_to_print)) - 1; i++) {
This one is a normal loop. It goes from 1 to the 2 less than length of the array, incrementing by 1 every time. Why things start at 1 will be discussed next.

Code:
string_to_print[i & 255] = (i & 255);
This one is unusually strange looking! Why not just use "i" rather than "i & 255" for example? The answer is simple. If we used "i", our string would never terminate (no null terminator). The "i & 255" ensures that it does by doing a bitwise AND. Also, if we had used "i = 0" instead of "i = 1", our string would have terminated immediately because that would mean the null terminator stops the string right at the beginning. More on the null terminator after the next code segment.

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;
This seems pretty straightforward, right? I mean, it makes the last element 0, but why is it there? When dealing with character arrays as strings, the last element MUST be a null terminator, which is character code 0 or the special escape code '\0'. The reason why is because there is no other way to determine where a string ends without adding some overhead to the code. Not only that, but it is very convenient when you need to truncate a string at the end so that only the beginning and part of the rest of the string is left.

The rest is easy to figure out.

To summarize:
  • There is no such thing as being crazy with parentheses in C++ or C, especially when working with "macros", where parentheses can make or break your macro's intended behavior.
  • C++ notation for constructs such as loops and branching are similar to, if not exactly like, the equivalent constructs in other languages such as Java.
  • When strings are actually character arrays, there must be one extra element allocated for the null terminator.
  • sizeof(<array_name>) / sizeof(*<array_name>) gets the length of an array that is not dynamic.
  • bitwise operators can be of use possibly

I hope this helps.

__________________

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
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-15-2007)
  #15 (permalink)  
Old 07-15-2007, 10:15 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Thanx a lot rpgfan, I think this should be an official tutorial or something
I really do understand the difference now.. One more thing though,

what does "&" (without quote) means in C++ or C languages???

__________________
PHP Code:
System.out.println("Hello World!"); 

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
  #16 (permalink)  
Old 07-15-2007, 01:35 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
Thanx a lot rpgfan, I think this should be an official tutorial or something
I really do understand the difference now.. One more thing though,

what does "&" (without quote) means in C++ or C languages???
Its official name in C and C++ is the "Bitwise AND" operator

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.

__________________

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
  #17 (permalink)  
Old 07-15-2007, 02:05 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
9 in binary is 1001
7 in binary is 0111

so 7 AND 9 yields 0001 (which is 1 in decimal)
Can you please explain me how does this example yields to 0001? do you substract them? Well, now I'm learning Discrete Math, but I never knew this.. XD

__________________
PHP Code:
System.out.println("Hello World!"); 

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
  #18 (permalink)  
Old 07-15-2007, 02:23 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
Can you please explain me how does this example yields to 0001? do you substract them? Well, now I'm learning Discrete Math, but I never knew this.. XD
Look at the vertical alignment of each digit on its own..

Code:
1  0  0  1
&  &  &  &
0  1  1  1

0  0  0  1
the result of AND only yields a '1' when both sides are a '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
although, unlike Bitwise-AND, the decimal math you learn in primary school has you worrying about digits "carrying over" aswell

__________________

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 Bench : 07-15-2007 at 02:36 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (07-15-2007)
  #19 (permalink)  
Old 07-15-2007, 02:25 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Thanx a lot buddy, I finally got it!! There's connection with symbolic logic right? Where 'true' and 'true' is 'true' while 'true' and 'false' is 'false' ???

__________________
PHP Code:
System.out.println("Hello World!"); 

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
  #20 (permalink)  
Old 07-15-2007, 03:34 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
Thanx a lot buddy, I finally got it!! There's connection with symbolic logic right? Where 'true' and 'true' is 'true' while 'true' and 'false' is 'false' ???
The idea is the exactly the same, yes. Although Conditional logic is used for an entirely different reason in C/C++ than bitwise logic..

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.

__________________

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 Bench : 07-15-2007 at 03:44 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (07-15-2007)
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 05:25 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