All computers have memory, also called RAM. RAM holds the programs your computer is currently running, along with the data the programs are currently manipulating. Each byte of memory has it's own address, the address of the first byte is 0, followed by 1, 2, 3... etc. Memory can group bytes together if it needs to form a larger variable, this happens when we declare an int variable.
An int variable is assigned four consecutive bytes in memory. When the program runs, the computer reserves space for that variable somewhere in memory, and from here on the data in the variable can be accessed by the computer by the address at which it is located.Consider the following:
Do you see? Now the computer knows that the data for number is stored in four consecutive memory addresses starting at 220, 126. Now if we declare a pointer and do:int *pointer = &number;
Memory could now look something like this:
The value we assigned to number is stored in its area in memory, and the pointer also has it's own place in memory. The thing about the pointer is that the data it contains is the address in memory of the variable to which is "points". If we change the value stored in the pointer, we change where it points. One thing i must mention, most computers today use 32-bit (4 byte) memory addresses. So the pointer is assigned 4 bytes of memory. Some computers use 64-bit memory addressing, meaning pointers on that computer would take up 8 bytes in memory.
But, why use them?
I asked the same question, it seems pointless, and in the above examples i suppose it is. I'm not showing you why pointers are used, i'm trying to explain how they're used, and to help you understand how they work. The advantages are pretty unclear, but as you progress and learn more about different data types you will realise the advantages. You can quickly change something with a pointer rather than changing much more data. It's just an easy way to work at the memory address level, manipulate data contained there, it allows you to manipulate data in memory more accurately. Make good use of pointers, they make C programs much more efficient.
Control Structures and Conditionals
There are a few control structures in C that you can make use of, very similar in syntax to those in Perl and PHP.If/Else
These allow you to execute code IF a boolean expression is true:
This speaks for itself really. If 1 is equal to 1, print out so, if not, print out that it isn't. Obviously 1 will always be equal to 1, so the printf() statement between the curly braces after the if() function will be executed.
If 1 wasn't equal to 1, then the else would be executed.
It's pretty easy to see what's going on here, these functions allow is to test if a boolen expression, or series of boolean expressions are true, and execute code depending on those tests.
No Comments for this page.