انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Pointers and References

الكلية كلية العلوم للبنات     القسم قسم الحاسبات     المرحلة 2
أستاذ المادة احمد علي حسين الجنابي       26/12/2015 20:04:27
Pointers and References
Understanding how pointers and references work is one step toward being able to
write programs that are effective in their consumption of system resources.
In this lesson, you find out
? What pointers are
? What the free store is
? How to use operators new and delete to allocate and free memory
? How to write stable applications using pointers and dynamic allocation
? What references are
? Differences between pointers and references
? When to use a pointer and when to use references
What Is a Pointer?
Put simply, a pointer is a variable that stores an address in memory. Just the same way
as a variable of type int is used to contain an integer value, a pointer variable is one
that is used to contain a memory address, as illustrated in the following Figure.
Thus, a pointer is a variable, and like all variables a pointer occupies space in memory
(in the case of Figure, at address 0x101). What’s special about pointers is that the
value contained in a pointer (in this case, 0x558) is interpreted as a memory address.
So, a pointer is a special variable that points to a location in memory.
Declaring a Pointer
A pointer being a variable needs to be declared, too. You normally declare a pointer
to point to a specific value type (for example, int). This would mean that the address
contained in the pointer points to a location in the memory that holds an integer. You
can also specify a pointer to a block of memory (also called a void pointer).
A pointer being a variable needs to be declared like all variables do:
PointedType * PointerVariableName;
As is the case with most variables, unless you initialize a pointer it will contain a
random value. You don’t want a random memory address to be accessed so you
initialize a pointer to NULL. NULL is a value that can be checked against and one that
cannot be a memory address:
PointedType * PointerVariableName = NULL; // initializing value
Thus, declaring a pointer to an integer would be:
int *pInteger = NULL; //
Determining the Address of a Variable Using the Reference Operator (&)
Variables are tools the language provides for you to work with data in the memory.
Pointers are variables, too, but they’re a special type that is used exclusively to contain
a memory address.
If VarName is a variable, &VarName gives the address in memory where its value is
placed.
So, if you have declared an integer, using the syntax that you’re quite well acquainted
with, such as:
int Age = 30;
&Age would be the address in memory where the value (30) is placed. Example below
demonstrates the concept of the memory address of an integer variable that is used
to hold the value it contains.

المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .