Search
Left Quote    Black holes are where God divided by zero.
- Steven Wright    
Right Quote
 
[login] | [Register]
 

C Tutorial

by: Jester
Page: 4 of 10
(View All)

Memory Addresses


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.



1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

27,479 Views
0 Total Comments
3.33 Rating of 5 (3 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Beginning Visual C++ 6.0
Your Article Here



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank