C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet745/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   741   742   743   744   745   746   747   748   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Pointer Arithmetic

There are only four arithmetic operators that can be used on pointers: 



++

,

– –

,

+

, and 


. To 


understand what occurs in pointer arithmetic, we will begin with an example. Let 

p1

 be an 


int

 pointer with a current value of 2,000 (that is, it contains the address 2,000). After this 

expression,

p1++;


www.freepdf-books.com


590

 

P a r t   I :  



T h e   C #   L a n g u a g e

the contents of 



p1

 will be 2,004, not 2,001! The reason is that each time 



p1

 is incremented, it 

will point to the 

next

int

. Since 


int

 in C# is 4 bytes long, incrementing 



p1

 increases its value 

by 4. The reverse is true of decrements. Each decrement decreases 

p1

’s value by 4. For 

example,

p1--;


will cause 

p1

 to have the value 1,996, assuming it previously was 2,000.

Generalizing from the preceding example, each time that a pointer is incremented, 

it will point to the memory location of the next element of its referent type. Each time it is 

decremented, it will point to the location of the previous element of its referent type.

Pointer arithmetic is not limited to only increment and decrement operations. You can 

also add or subtract integers to or from pointers. The expression

p1 = p1 + 9;

makes

p1

 point to the ninth element of 



p1

’s referent type, beyond the one it is currently 

pointing to.

Although you cannot add pointers, you can subtract one pointer from another (provided 

they are both of the same referent type). The remainder will be the number of elements of 

the referent type that separate the two pointers.

Other than addition and subtraction of a pointer and an integer, or the subtraction of 

two pointers, no other arithmetic operations can be performed on pointers. For example, 

you cannot add or subtract 

float

 or 


double

 values to or from pointers.

To see the effects of pointer arithmetic, execute the next short program. It prints the 

actual physical addresses to which an integer pointer (



ip

) and a floating-point pointer (



fp

)

are pointing. Observe how each changes, relative to its referent type, each time the loop is 



repeated.

// Demonstrate the effects of pointer arithmetic.

using System;

class PtrArithDemo {

  unsafe static void Main() {

    int x;

    int i;

    double d;

    int* ip = &i;

    double* fp = &d;

    Console.WriteLine("int     double\n");

    for(x=0; x < 10; x++) {

       Console.WriteLine((uint) (ip) + " " + (uint) (fp));

       ip++;

       fp++;

    }


  }

}

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   741   742   743   744   745   746   747   748   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish