关闭

Pointer in C/C++

353人阅读 评论(0) 收藏 举报

Pointers

    The source code to all code listings is available as a tarball and as a zip file.

Using Variables

Essentially, the computer's memory is made up of bytes.  Each byte hasa number,an address, associated with it.

The picture below represents several bytes of a computer's memory.  In the picture, addresses 924 thru 940 are shown.

Try:

C Code Listing 1
 #include <stdio.h>
 int main()
 {
   float fl=3.14;
   printf("%.2f\n", fl);
   return 0;
 }


C++ Code Listing 1
 #include <iostream>
 int main()
 {
   float fl=3.14;
   std::cout << fl << std::endl;
   return 0;
 }


 

At line (4) in the program above, the computer reservesmemory for fl.  In our examples, we'll assumethat afloat requires 4 bytes.  Depending on thecomputer's architecture, afloat may require 2,4, 8 or some other number of bytes.

When fl is used in line (5), two distinct steps occur:

     
  1. The program finds and grabs the address reserved for fl--in this example 924. 
  2. The contents stored at that address are retrieved

To generalize, whenever any variable is accessed,the above two distinct steps occur to retrieve the contentsof the variable.

Separating the Steps

Two operators are provided that, when used, cause these two steps tooccur separately.

 
operator meaning example
& do only step 1 on a variable &fl
* do step 2 on a number(address) *some_num

Try this code to see what prints out:

C Code Listing 2
#include <stdio.h>
int main()
{
   float fl=3.14;
   printf("fl's address=%u\n", (unsigned int) &fl);
   return 0;
}

C++ Code Listing 2
#include <iostream>
int main()
{
   float fl=3.14;
   std::cout << "fl's address=" << (unsigned int) &fl << std::endl;
   return 0;
}

On line (5) of the example, The & operator is being usedon fl.  On line (5), only step 1 is being performed ona variable:

1. The program finds and grabs the address reserved for fl...

It is fl's address that is printed to the screen.If the & operator had not been placed in frontoffl, then step 2 would have occurred as well,and 3.14 would have been printed to the screen.


Keep in mind that an address is really just a simple number.  In fact, we can store an address in an integer variable.  Try this:

C Code Listing 3
#include <stdio.h>
int main()
{
   float fl=3.14;
   unsigned int addr=(unsigned int) &fl;
   printf("fl's address=%u\n", addr);
   return 0;
}

C++ Code Listing 3
#include <iostream>
int main()
{
   float fl=3.14;
   unsigned int addr=(unsigned int) &fl;
   std::cout << "fl's address=" << addr << std::endl;
   return 0;
}

The above code shows that there is nothing magical about addresses.They are just simple numbers that can be stored in integer variables.


Now let's test the other operator, the * operator that retrieves the contents stored at an address:

C Code Listing 4
#include <stdio.h>
int main()
{
   float fl=3.14;
   unsigned int addr=(unsigned int) &fl;
   printf("fl's address=%u\n", addr);
   printf("addr's contents=%.2f\n", *  (float*) addr);
   return 0;
}

C++ Code Listing 4
#include <iostream>
int main()
{
   float fl=3.14;
   unsigned int addr=(unsigned int) &fl;
   std::cout << "fl's address=" << addr << std::endl;
   std::cout << "addr's contents=" << * (float*) addr << std::endl;
   return 0;
}

In line (7), step 2 has been performed on a number:

2. The contents stored at that address [addr] are retrieved.

OK, But why do we need & and *

We have shown that 2 distinct steps occur when accessing a variable, and that we can make those steps occur separately.  But why is this useful?

To see why, let's first look at how functions work in C/C++.  Try this code:

C Code Listing 5

#include <stdio.h>
void somefunc(float fvar)
{
   fvar=99.9;
}
int main()
{
   float fl=3.14;
   somefunc(fl);
   printf("%.2f\n", fl);
   return 0;
}

C++ Code Listing 5

#include <iostream>
void somefunc(float fvar)
{
   fvar=99.9;
}
int main()
{
   float fl=3.14;
   somefunc(fl);
   std::cout << fl << std::endl;
   return 0;
}


What prints out?  3.14?  99.9?  It turns out that 3.14 prints out.  The general term used to describe this behavior ispass by value.  Whensomefunc(fl) is called at line 9:

  1. Execution jumps to line (2) to run the function
  2. fvar is created as its own variable and fl's value is copied intofvar
  3. On line (4), 99.9 is assigned to fvar
  4. Now that the function is finished, execution resumes in main where it left off (line 10).  Thefl variable is unchanged, 3.14 prints out.

We can circumvent this pass by value behavior and change valuespassed into functions by using the& and*operators.

C Code
#include <stdio.h>
void somefunc(unsigned int fptr)
{
   *(float*)fptr=99.9;
}


int main()
{
   float fl=3.14;
   unsigned int addr=(unsigned int) &fl;
   somefunc(addr);
   printf("%.2f\n", fl);
   return 0;
}


C++ Code
#include <iostream>
void somefunc(unsigned int fptr)
{
   *(float*)fptr=99.9;
}

 int main()
 {
   float fl=3.14;
   unsigned int addr=(unsigned int) &fl;
   somefunc(addr);
   std::cout << fl << std::endl;
   return 0;
 }



Quite simply, the two steps that normally occur when accessing a variableare being separated to allow us to change the variable's value in adifferent function.

  1. The floating point variable  fl is created at line (9) and giventhe value 3.14
  2. The & operator is used on fl at line (10) (do onlystep 1, get the address).  The address is stored in the integer variableaddr.
  3. The function somefunc is called at line (11)and fl's address is passed as an argument.
  4. The function somefunc begins at line (2),fptr is created andfl's address is copiedintofptr.
  5. The * operator is used on fptr at line (4) -- do step 2, the contents stored in an address are retrieved.  In this example, the contents at address 924 are retrieved.
  6. The contents at address 924 are assigned the value 99.9.
  7. The function finishes.  Control returns to line (12).
  8. The contents of fl are printed to the screen.

Pointer Variables

Even though we have shown that an address is nothing more than a simpleinteger, the creators of the language were afraid we might confusevariables in our programs.  We might confuse integers we intend to usefor program values (e.g. variables storing ages, measurements, counters,etc.) with integers we intend to use for holding the addresses of ourvariables.

The language creators decided the best way to eliminate confusion was to create a different type of variable for holding addresses.  A first attempt at this might have looked something like this:

...
 float fl=3.14;
  float Ptr addr = &fl;
...

On line (3), here is how to describe the addr variable:

(A) addr is an integer.  (B) However, it is a special integer designed to hold the address of a(C)float

In the code above, line (3) is close to what the creators of the languagewanted except for one thing:  usingPtr would requireintroducing another keyword into the language.  If there is one thingthat all C instructors like to brag about, it is how there are onlya very small number of keywords in the language.  Well, using line (3)as shown above would mean addingPtr as another keyword tothe language.

To avoid this threat to the very fabric of the universe, the creatorscast about for something already being used in the language that coulddo double duty asPtr shown above.  What they came up withwas the following:

...
  float fl=3.14;
  float * addr = &fl; 
...

Even with the * instead of Ptr, addr is described the same way:

(A) addr is an integer.  (B) However, it is a special integer designed to hold the address of a(C)float

These variables are described this way, regardless of the type:


(A) addr is an integer.  (B) However, it is a special integer designed to hold the address of a(C)char


(A) addr is an integer.  (B) However, it is a special integer designed to hold the address of an(C)int

This "...special integer..." way of describing these variables is amouthful, so we shorten it and just say "addr is a float pointer" or"addr is a pointer to a float" (or char, or int, etc.).

Unfortunately, the language creators chose the * characterto replace Ptr.  The* character is confusing because the* character is also used to get the contents at an address("do step 2 on a number"). These two uses of the *character have nothing to do with each other.

What is all that "syntax sugar" anyway?  (Casting)

Let's take one last look at our original code that illustrates theutility of separating out steps 1 & 2.

C Code Listing 7
#include <stdio.h>
void somefunc(unsigned int fptr)
{
   *(float*)fptr=99.9;
}

int main()
{
   float fl=3.14;
   unsigned int addr=(unsigned int) &fl;
   somefunc(addr);
   printf("%.2f\n", fl);
   return 0;
}

C++ Code Listing 7
#include <iostream>
void somefunc(unsigned int fptr)
{
   *(float*)fptr=99.9;
}

int main()
{
   float fl=3.14;
   unsigned int addr=(unsigned int) &fl;
   somefunc(addr);
   std::cout << fl << std::endl;
   return 0;
}

In nearly all of the code samples, you have been asked toignore certain bits of the code.  These bits of code havealways appeared around those areas where we are either takingthe address of a variable or getting the contents at an address(doing step 1 or step 2 on a variable)

Those bits of "syntax sugar" are there to keep the compilerfrom complaining.  The first example of this in the aboveprogram is on line (10).

On line (10) we are taking the address of the floatingpoint number fl ("do only step 1 on a number").After we get that address, we store it inaddr.

Why would the compiler complain?  Because when we assign theaddress of fl toaddr, the compilerdoes not expectaddr to be anunsignedint.  The compiler expectsaddr to be afloat *.  That is,a special integer designedto hold the address of a float.  To keep the compiler fromcomplaining, we tell the compiler to treat&fl asanunsigned int rather than afloat *.

This "syntax sugar" that causes the compiler to treatvariables and expressions differently is calledcasting.The way a programmer describes line (10) is: "The address offl is beingcast intoanunsigned int and assigned to addr"

The other place casting occurs is on line (4).  On line (4),we are getting the contents at an address ("do step 2 on anumber/address").  Why would the compiler complain?  Because thecompiler should get the contents of the address of a float.The address of our float is in stored in fptr,which is an unsigned int, not afloat*.  We tell the compiler to treatfptras the address of a floating point number by casting it into afloat *.  Once we tell the compiler this, we canget the contents at the address without complaint.

Putting it all together

From the previous section, you might be left with the impressionthat whenever you deal with addresses and pointers, there isa lot of casting.  Not so.  The only reason our examples uptill now have required casting is because we were storing ouraddresses in unsigned int variables.  The languagedesigners want us to store addresses in the "special integer"variables, that is, the pointer variables they designed forjust such a purpose.

Once we replace our unsigned int variableswith these pointer variables, none of the castingis required:

C Code Listing 8
#include <stdio.h>
void somefunc(float* fptr)
{
   *fptr=99.9;
}

int main()
{
   float fl=3.14;
   float* addr = &fl;
   somefunc(addr);
   printf("%.2f\n", fl);
   return 0;
}


 
C++ Code Listing 8
#include <iostream>
void somefunc(float* fptr)
{
   *fptr=99.9;
}

int main()
{
   float fl=3.14;
   float* addr = &fl;
   somefunc(addr);
   std::cout << fl << std::endl;
   return 0;
}

  • On line (10), when we take the address of flthe address is assigned to a variable designed to holdit.  No casting is required.
  • When addr is passed to the function in line (11),addr is copied tofptr on line(2).
  • Line (2) shows that fptr is created as a floatpointer, that is a variable designed to hold the address ofa floating point number.  As a result, no casting is neededon line (4) where the contents at the address are retrieved.
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:601298次
    • 积分:9013
    • 等级:
    • 排名:第2075名
    • 原创:259篇
    • 转载:289篇
    • 译文:1篇
    • 评论:102条
    最新评论
    数据结构