C# 0 The Complete Reference



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

Pointers and Strings

Although strings are implemented as objects in C#, it is possible to access the characters in 

a string through a pointer. To do so, you will assign a pointer to the start of the string to a 

char*

 pointer using a 



fixed

 statement like this:

fi xed(char* 

p

 = 


str

) { // ...

After the 

fixed

 statement executes



p

 will point to the start of the array of characters that 

make up the string. This array is 

null-terminated,

 which means that it ends with a zero. You 

can use this fact to test for the end of the array. Null-terminated character arrays are the way 

that strings are implemented in C/C++. Thus, obtaining a 



char*

 pointer to a 



string

 allows 


you to operate on strings in much the same way as does C/C++.

Here is a program that demonstrates accessing a string through a 



char*

 pointer:

// Use fixed to get a pointer to the start of a string.

using System;

class FixedString {

  unsafe static void Main() {

    string str = "this is a test";

www.freepdf-books.com




594

 

P a r t   I :  



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

    // Point p to start of str.

    fixed(char* p = str) {

      // Display the contents of str via p.

      for(int i=0; p[i] != 0; i++)

        Console.Write(p[i]);

    }

    Console.WriteLine();



  }

}

The output is shown here:



this is a test

Multiple Indirection

You can have a pointer point to another pointer that points to the target value. This situation 

is called 

multiple indirection,

 or 


pointers to pointers.

 Pointers to pointers can be confusing. 

Figure 20-1 helps clarify the concept of multiple indirection. As you can see, the value of a 

normal pointer is the address of the variable that contains the value desired. In the case of a 

pointer to a pointer, the first pointer contains the address of the second pointer, which points 

to the variable that contains the value desired.

Multiple indirection can be carried on to whatever extent desired, but more than a 

pointer to a pointer is rarely needed. In fact, excessive indirection is difficult to follow and 

prone to conceptual errors.

A variable that is a pointer to a pointer must be declared as such. You do this by placing 

an additional asterisk after the type name. For example, the following declaration tells the 

compiler that 



q

 is a pointer to a pointer of type 



int

:

int** q;



You should understand that 

q

 is not a pointer to an integer, but rather a pointer to an 



int

pointer.


F

IGURE


 20-1 

Single and multiple indirection

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   744   745   746   747   748   749   750   751   ...   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