Your computer memory is divided into a bunch of “boxes” and each “box” has a size of 1 byte. You must know about, at least, the basic model of memory in order to use or understand pointers. Let’s first look at a simple variable.
int x;
In the above line, we are declaring a variable called x which is of an integer type. A computer must somehow store this variable in order to use it in the future. So it stores this variable in the memory. But where? This type of simple variable is stored somewhere in your program’s memory space. An integer has a size of 4 bytes (in old compilers it is 2 bytes), so it’ll occupy 4 “boxes” of your memory. Nothing else can use this memory space because it holds your variable unless your program exists. Yes! You can’t “empty” this memory space after you have put your variable x in there. A memory model containing x would like this:
| | | | |
Right now there’s nothing in those 4 “boxes” but as you assign some number to x, some or all of these boxes will be filled up according to the size of your number.
x = 4;
Now you have assigned a value of 4 to x. You can retrieve the value by writing x like this:
printf("%d",x);
So we can conclude that a value is some sort of data that is held in the memory space reserved for your variable.
Above theory shows you how variable contains a value and how it’s organized in the memory. Now let's talk about a simple array.
int x[4];
The above line declares an array of 4 integer. How much memory do you think is reserved for this variable (for now)? It’s not 4 bytes but 16 bytes!! Now at the same time Let's start talking about pointers...
A pointer is a variable, as usual, but it’s VALUE is an address of some other variable. You declare a pointer with prefix of * like this:
int *p;
Now if we want to access the value of p, we simply use the name “p”. But the pointer’s value will holds some memory address. It is usually the address of another variable but not always!
The above code will print a hexadecimal address of x. See! The actual value of our pointer p holds an address. Now try this:
int x = 3; //Declare a variable
int *p; //Declare a pointer
p = &x; //p now holds the memory address of x
printf("%d",*p);
You use the “&” symbol to access the memory address of a variable - NOT THE VALUE! The above statement will print the actual value of x. But we didn’t really use the variable x to get the value. Because p already holds the address of x, you can use the prefix * to retrieve whatever is on the address that p holds. When we used ,*p, it actually looks at the value of p. The value of p is actually an address of some place in the memory so *p returns the value on that address.
A pointer has 3 “things” compared to a variable which only has 2 “things”. A variable has a value and an address while a pointer has a value (address of some variable), its own memory address (we can get it using &p), and a pointed value which it gets by looking at its value (again, the memory address of another variable). We get the value using *p as we wrote above.
Let’s declare an array now:
int x[4];
It’s 16 bytes long in memory. The start of this array is x[0] so we can get the starting address of this array by using &x[0] (remember you use the “&” sign to get the address of a variable). Now remember this equation for an array (x is an array in this example):
x = &x[0];
So if we have an array x, we can simply use x to retrieve the starting address of the array in the memory. We can now simply declare a pointer that points to the start of the array thus the value of our pointer will be the starting address of the array in the memory.
int x[4] = {1,2,3,4};
int *p;
p = x;
Bookmarks