1. Imagine a spreadsheet. In a spreadsheet, columns have letters and rows have numbers, correct? This is so E42 refers to one and only one cell. An array with more than one dimension is exactly like this, only you refer to "cells" (elements, more properly) using only numbers:
1 2
|
int array[32][32];
array[row][column]=42;
| |
2.
All i think of when a book explains this is, if i was going to use count, i would type count |
Except you don't always know what the pointer is pointing to.
Suppose you're in front of a door, and you know that Alice and Bob are in the room behind it. You think "I will talk to the first person that comes out that door". You can't possibly know in advance who will come out first, so both "I will talk to Alice" and "I will talk to Bob" are not suitable in this case.
Translated to code:
1 2 3 4 5 6 7 8
|
void f(person *p){
talk_to(*p);
}
//...
if (random_bool())
f(&alice);
else
f(&bob);
| |
This is just one very simple example of a problem that is otherwise unsolvable (in C) without pointers.
every part in a book that explains using pointers in future explanations (that i read) say there is another way too ( and i just think OK i will just do that then) but then the book says you need to learn pointers. |
That other way is references. They aren't a substitute for pointers in all situations (in my example above, they are), so you still need to know how pointers work.
3. See my previous example. It has an equivalent writing in references, with the exception that references wouldn't allow certain programming errors to come up.
4.
I can make void functions, but then i just make a global variable and that seems to make returning a variable from a function moot. |
Consider this example:
1 2 3 4 5 6 7 8 9 10 11 12
|
int x;
void square_x(){
x=x*x;
}
//...
int a=5,
b;
//...
x=a;
square_x();
b=x;
| |
Alright. That works. You can square any number you want. There's a problem, though. The point of functions is that they're supposed to eliminate duplicate code. Every time you want to square a number, you have to fill x, call the function, then extract the result from x. If you were using functions properly, the compiler would generate machine code that actually performs pretty much these operations (although the machine code is actually nicer than what you're doing because it doesn't use globals). Not only is this ugly, it's also error-prone.
Every time you call the function, you have to remember to set the variables properly, and the compiler can't detect your mistakes. One slip up, and your output will just be garbage.
Take this, on the other hand:
1 2 3 4 5
|
int square(int x){
return x*x;
}
//...
b=square(a);
| |
The code has the same behavior as before, only now you can't possibly forget to pass the parameter. The compiler won't let you forget.
These, as all examples you'll find in books (at least at the beginning) are simple because they're meant to just show the basics of how to use some feature. In a complete program with thousands and thousands of lines, these seemingly trivial problems become humongous. It's to be expected that you can't make that extrapolation from such simple examples, but think about it like this: would this feature really exist if there was a much simpler solution? Did the designers of this language just think up the most complicated solution because they're sadists, or am I just missing some case were something this complex would actually come in handy?
a) how can a c++ program output picures or animations ? |
There's no general answer for this. For example, a picture could be drawn as ASCII art on the console, and it would be, from the point of view of the code, basically the same and displaying graphics.
The best answer you have is: the program asks the windowing system to put graphics on the screen. In the case of bitmaps, it's possible to give the system a pointer to an array of pixel data with some other information (pixel format, dimensions, etc.), and the system does the drawing however it sees fit. How the drawing is actually performed depends on a variety of factors that aren't really important and are generally beyond the program's control.
b) how can people make a game (even so simple as something that i can make) and have it for download from interent (without the use of giving away the source code for it? |
See:
http://en.wikipedia.org/wiki/Compiler
http://en.wikipedia.org/wiki/Machine_language
In summary: a compiler (to native code) is a program that transforms source code into a representation that the computer can directly execute without any other information. The computer doesn't understand C++ or any other language, so the source code is unnecessary.
c) emp.wage = 123.23; AND emp->wage = 123.23 some books start explaining this early on "all i think is WTF are they talking about?" |
None of that will make any sense until you understand pointers.
d) bitwise operators. some books start explaining this with massive amount of 0's and 1's over top of each other, "again WTF" |
There isn't really any other way of explaining it. The computer represents numbers as a series of binary digits. For example, 10 could be 00001010 and 255 could be 11111111. You know how to add or multiply two decimal numbers, correct? You align the numbers one on top of the other and you do some manipulations with the digits and at the end that gives you an answer that makes sense to you. Bitwise operations are like that to a computer, only they're based on the binary system, and the manipulations are actually much simpler.
In the following paragraphs, I will use * and + to emphasize that these are just like arithmetic operations, but keep in mind that we're working in binary, and that they're not actually multiplication and addition; although they will look a lot like them.
First consider these simple facts:
a * b = r
0 * 0 = 0
0 * 1 = 0
1 * 0 = 0
1 * 1 = 1
a + b = r
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 1
With these simple rules, we can extend * and + to binary numbers of any length. Let's say we want to get 101*011:
101 *
011 =
???
We can start from the left or right; there are no carries, so it makes no difference. I'll start from the left.
1*0=0
101 *
011 =
0??
0*1=0
101 *
011 =
00?
1*1=1
101 *
011 =
001
Thus, we get that 101*011=001. We can apply the same method to get 101+011:
1+0=1
101 +
011 =
1??
0+1=1
101 +
011 =
11?
1+1=1
101 +
011 =
111
I said that * and + are not multiplication and addition. They're the bitwise AND (&) and OR (|) operators used in C/++. The rules I laid out before that defined how * and + behave with single bits are called "truth tables". The truth tables are same as those of logical/boolean conjunction (r is true if both a
AND b are true) and disjunction (r is true if a
OR b or both are true).
http://en.wikipedia.org/wiki/Logical_disjunction
http://en.wikipedia.org/wiki/Logical_conjunction
If you're familiar with sets in mathematics, AND is similar to a set intersection, and OR is similar to a set union.
Aside: Huh. I just realized that ({0,1},&,|) makes a field. I had never noticed that before. Maybe that's why electronics people write them as * and +.