int var_x;
int* ptrX;
var_x = 6;
ptrX = &var_x;
*ptrX = 12;
printf("value of x : %d", var_x);
The first line causes the compiler to reserve a space in memory for a integer.
The second line tells the compiler to reserve space to store a pointer. As you can see the way you
declare a pointer is simply to add a "*" asterick to the end of the data type. A pointer is a storage location for an address.
The third line assigns the value 6 to integer var_x.
The fourth line should remind you of the scanf statements. The address "&"
operator tells C to goto the place it stored var_x, and then give the address of the
storage location to ptrX.
The fifth line is a bit more complex. An asterick * in front of
a variable tells C to dereference the pointer, and go to memory. Then you can make assignments
to variable stored at that location. Since ptrX points to var_x, line 5 is equivalent to this
command: var_x = 12; Pretty cool, eh? You can reference a variable
and access its data through a pointer. Windows and all sorts of programs do this all the time.
They hand pointers of data to each other, and allow other applications to access the memory they
have.
Now you may be wondering, why are pointers so comlpex, or I've heard that using pointers can
cause problems. It can, and for those who aren't careful misuse of pointer can do a lot of damage.
Suppose that we forget to type in line 4 ptrX = &var_x; when we
entered the program. What would happen if we executed it, who knows? Without this line ptrX is
never assigned an address. Basically it points to some random data anywhere in memory. If you
executed line 5 without line 4. You could get very wierd results. Since ptrX hasn't been pointed
to our var_x, maybe its points to system memory, and then you assign a value someplace
you shouldn't and your computer crashes. This may not always happen, but it is certainly a possibility,
so be very careful when using pointers. Make sure they are assigned to something before you use
them.
With that basic understanding of pointers, it is time to move to arrays. The most obvious
use of arrays would be an array of characters also commonly knows as a string. The following
program will make a string, access some data in it, print it out. Access it again using
pointers, and then print the string out. It should print out "Hi!" and "012345678" on different lines.
The explanation of the code and arrays will follow.
#include <stdio.h>
#define STR_LENGTH 10
void main()
{
char Str[STR_LENGTH];
char* pStr;
int i;
Str[0] = 'H';
Str[1] = 'i';
Str[2] = '!';
Str[3] = '\0'; // special end string character
printf("The string in Str is : %s\n", Str);
pStr = &Str[0];
for (i = 0; i < STR_LENGTH; i++)
{
*pStr = '0'+i;
pStr++;
}
Str[STR_LENGTH-1] = '\0';
printf("The string in Str is : %s\n", Str);
}
First off, let's talk about the array notation to declare an array in C, you use [] square braces.
The line of the program char Str[STR_LENGTH]; declares an array of ten characters. Basically this
is just ten individual chars which are all put together in memory into the same place. An apartment complex in memory
to use our pointer metaphor. They can all be access through our variable name Str along with a
[n] where n is the element number (apartment number at same address). Also notice that when C
declares an array of ten. The elements you can access are numbered 0 to 9. Accessing the first apartment corresponds to accessing
the zeroeth element in C. Arrays are always like this, so learn to deal with it. Always count from 0 to size of array - 1.
Next notice that we put the letters "Hi!" into the array, but then we put in a '\0' You are probably wondering what this is.
If you recall in lesson two on printf, there are special charcter sequences that do special things like "\n" stands for new line.
Well time to learn a new one. "\0" stands for end string. All character strings need to end with this
special character '\0'. If they do not, and then someone calls printf on the string. Then printf would
start at the memory location of your string, and continue printing tell it encounters '\0' So you will end up with a
bunch of garbage at the end of your string. So make sure to terminate your strings properly.
The next part of the code to discuss is our pointer access to the string. Just like we learned, I declared a character pointer with an
asterick and gave it the name pStr. I then pointed pStr to the starting address of our character string using the line
pStr = &Str[0];. Now pStr points to the start of our char array Str.
Then I used a for loop, and started at 0, went through 10 elements of the array (STR_LENGTH) and assigned the corresponding value of i.
The line pStr++; may seem a bit confusing. C has a bunch of short cuts to manipulate variables the ++ just means
add one to the variable (in this case it moves the pointer to the next element in the array). The ++ syntax here is equivalent to
pStr = pStr + 1;. After manipulating the string, I terminated it with '\0' and printed it out. That about
does it for pointers and arrays, here are a few quick notes.
You should note that you will see other shortcuts in C like -- (subtracts one) or +=3; (adds three). I won't
bother covering them, since you should be able to figure them out just by looking at them. Another note is that you can make arrays of any
of C's types, I just used char arrays since they seem to be the most common. Here is a sample line to make an array of five integers: int arrayofint[5];.
|