Monday, January 26, 2009

Pointers

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 has a 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
1:#include
2:int main()
3:{
4: float fl=3.14;
5: printf("%.2f\n", fl);
6: return 0;
7:}
C++ Code Listing 1
1:#include
2:int main()
3:{
4: float fl=3.14;
5: std::cout <<>
2: int main()
3: {
4: float fl=3.14;
5: printf("fl's address=%u\n", (unsigned int) &fl);
6: return 0;
7: }
C++ Code Listing 2
1:#include
2: int main()
3: {
4: float fl=3.14;
5: std::cout << "fl's address=" << (unsigned int) &fl <<>
2: int main()
3: {
4: float fl=3.14;
5: unsigned int addr=(unsigned int) &fl;
6: printf("fl's address=%u\n", addr);
7: return 0;
8: }
C++ Code Listing 3
1: #include
2: int main()
3: {
4: float fl=3.14;
5: unsigned int addr=(unsigned int) &fl;
6: std::cout << "fl's address=" <<>
2: int main()
3: {
4: float fl=3.14;
5: unsigned int addr=(unsigned int) &fl;
6: printf("fl's address=%u\n", addr);
7: printf("addr's contents=%.2f\n", * (float*) addr);
8: return 0;
9: }
C++ Code Listing 4
1: #include
2: int main()
3: {
4: float fl=3.14;
5: unsigned int addr=(unsigned int) &fl;
6: std::cout << "fl's address=" << contents=" << * (float*) addr << std::endl; 8: return 0; 9: } In line (7), step 2 has been performed on a number: 2. The contents stored at that address [addr] are retrieved. In order to make line (7) work, a little ">
2: void somefunc(float fvar)
3: {
4: fvar=99.9;
5: }
6: int main()
7: {
8: float fl=3.14;
9: somefunc(fl);
10: printf("%.2f\n", fl);
11: return 0;
12: }
C++ Code Listing 5
1: #include
2: void somefunc(float fvar)
3: {
4: fvar=99.9;
5: }
6: int main()
7: {
8: float fl=3.14;
9: somefunc(fl);
10: std::cout <<>
2: void somefunc(unsigned int fptr)
3: {
4: *(float*)fptr=99.9;
5: }
6:
7: int main()
8: {
9: float fl=3.14;
10: unsigned int addr=(unsigned int) &fl;
11: somefunc(addr);
12: printf("%.2f\n", fl);
13: return 0;
14: }
C++ Code
1: #include
2: void somefunc(unsigned int fptr)
3: {
4: *(float*)fptr=99.9;
5: }
6:
7: int main()
8: {
9: float fl=3.14;
10: unsigned int addr=(unsigned int) &fl;
11: somefunc(addr);
12: std::cout << fl="3.14;" addr =" &fl;" fl="3.14;" addr =" &fl;">
2: void somefunc(unsigned int fptr)
3: {
4: *(float*)fptr=99.9;
5: }
6:
7: int main()
8: {
9: float fl=3.14;
10: unsigned int addr=(unsigned int) &fl;
11: somefunc(addr);
12: printf("%.2f\n", fl);
13: return 0;
14: }
C++ Code Listing 7
1: #include
2: void somefunc(unsigned int fptr)
3: {
4: *(float*)fptr=99.9;
5: }
6:
7: int main()
8: {
9: float fl=3.14;
10: unsigned int addr=(unsigned int) &fl;
11: somefunc(addr);
12: std::cout <<>
2: void somefunc(float* fptr)
3: {
4: *fptr=99.9;
5: }
6:
7: int main()
8: {
9: float fl=3.14;
10: float* addr = &fl;
11: somefunc(addr);
12: printf("%.2f\n", fl);
13: return 0;
14: }
C++ Code Listing 8
1: #include
2: void somefunc(float* fptr)
3: {
4: *fptr=99.9;
5: }
6:
7: int main()
8: {
9: float fl=3.14;
10: float* addr = &fl;
11: somefunc(addr);
12: std::cout << fl << std::endl;
13: return 0;
14: }
• On line (10), when we take the address of fl the address is assigned to a variable designed to hold it. No casting is required.
• When addr is passed to the function in line (11), addr is copied to fptr on line (2).
• Line (2) shows that fptr is created as a float pointer, that is a variable designed to hold the address of a floating point number. As a result, no casting is needed on line (4) where the contents at the address are retrieved.

Cigars - All your favorite cigars online at wholesale.
Premium Cigars - Premium cigars at discount prices.

Kia Dealers - Get multiple Kia price quotes from competing Kia Dealers
Used Autos - Autotropolis.com is your #1 stop for auto reviews and comparisons on new automobiles and trucks.

No comments:

Post a Comment