C# 0 The Complete Reference


/unsafe  compiler option. Since pointers are at the core of unsafe code, we will begin there. Pointer Basics



Download 4,07 Mb.
Pdf ko'rish
bet739/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   735   736   737   738   739   740   741   742   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

/unsafe

 compiler option.

Since pointers are at the core of unsafe code, we will begin there.

Pointer Basics

A pointer is a variable that holds the address of some other object, such as another variable. 

For example, if 

x

 contains the address of 



y

, then 


x

 is said to “point to” 



y

. When a pointer 

points to a variable, the value of that variable can be obtained or changed through the pointer. 

Operations through pointers are often referred to as 



indirection.

Declaring a Pointer

Pointer variables must be declared as such. The general form of a pointer variable 

declaration is

type

*

var-name

;

Here, 


type

 is the pointer’s 



referent type,

 which must be a nonreference type. Thus, you cannot 

declare a pointer to a class object. A pointer’s referent type is also sometimes called its 

base

type.

 Notice the placement of the 



*

. It follows the type name. 



var-name

 is the name of the 

pointer variable.

Here is an example. To declare 



ip

 to be a pointer to an 



int

, use this declaration:

int* ip;

For a 


float

 pointer, use

float* fp;

In general, in a declaration statement, following a type name with an 



*

 creates a pointer type.

The type of data that a pointer will point to is determined by its referent type. Thus, in 

the preceding examples, 



ip

 can be used to point to an 



int

, and 


fp

 can be used to point to a 



float

. Understand, however, that there is nothing that actually prevents a pointer from 

pointing elsewhere. This is why pointers are potentially unsafe.

If you come from a C/C++ background, then you need to be aware of an important 

difference between the way C# and C/C++ declare pointers. When you declare a pointer 

type in C/C++, the 



*

 is not distributive over a list of variables in a declaration. Thus, in 

C/C++, this statement

int* p, q;

declares an 

int

 pointer called 



p

 and an 


int

 called 


q

. It is equivalent to the following two 

declarations:

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   735   736   737   738   739   740   741   742   ...   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