The C Programming Language - Pointers
This is a free tutorial about pointers from the book "The C Programming Language" by Heimo Gaicher
This is a free tutorial about pointers from the book "The C Programming Language" by Heimo Gaicher
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
1 <strong>Pointers</strong><br />
<strong>Pointers</strong> are a very important tool in the C programming language. Basically, a pointer<br />
is nothing more than a variable that stores the memory address of another variable.<br />
So, pointers only store the address of a memory object of a certain data type.<br />
If you've worked with functions, you may have thought that it would be handy if you<br />
could return more than one value to the calling function.<br />
This is easily done with a pointer. In this case, you need to pass the address, that is, a<br />
pointer to a specific memory location, to the function. You can now use this address<br />
to write a desired value to the memory location specified in the function. You have<br />
now overwritten the variable at the corresponding memory location with a new value<br />
and can also use this variable outside the function.<br />
Another advantage of pointers is the saving of copying operations and the associated<br />
saving of computing time. Suppose you have a function to which you pass ten integer<br />
values. With a 16-bit system, you have to pass 10 x 2 bytes here. But if you use a pointer<br />
here, you only pass a pointer to the beginning of an array of these ten integer values.<br />
And for this pointer you need only 2 bytes of memory.<br />
Also, memory areas can be dynamically manipulated with pointers. You can pass data<br />
objects by reference to functions, and you can use pointers to implement complex data<br />
structures like lists.<br />
So, pointers contain a memory address of a certain expected data type, e.g. char, int,<br />
float, etc. A pointer points directly to a specific address in memory. <strong>Pointers</strong> are<br />
dereferenced with the * operator. <strong>The</strong> & operator returns the address of an element<br />
to which the & operator is applied.<br />
Let's first look at how a variable is stored in memory:<br />
int a = 2;<br />
Here the variable a of data type integer has been initialized with the value 2. In this<br />
example, this variable was stored in RAM, e.g., at address 0x015A.<br />
Name Address Value<br />
a 0x015A 2<br />
<strong>The</strong> C <strong>Programming</strong> <strong>Language</strong> by Heimo Gaicher – Chapter <strong>Pointers</strong> 6