Professional Documents
Culture Documents
POINTERS
Programming Fundamentals
Spring 2019
Week 16
Outline
■ Introduction
■ Pointer
– Reference operator (&)
– Deference operator (*)
– Declaration of Pointer variables
■ Pointers and Arrays
■ Pointer Arithmetic
■ Pointer to Pointer
■ Pointer and Functions
■ Summed up -Course
10.1. Introduction
… … 100 … 1024 …
a
Variable a’s value, i.e., 100, is
int a = 100; stored at memory location 1024
10.2 Pointers
■ The variable that stores the reference to another variable is called a pointer.
■ A pointer is a variable used to store the address of a memory cell.
■ We can use the pointer to reference this memory cell
int a = 100;
int *p;
Memory address: 1020 1024 1032
… … 100 … 1024 …
a p
integer
pointer
10.2.1 Reference Operator (&)
■ The "address of " operator (&) gives the memory
address of the variable
– Usage: &variable_name
… … 100 … … …
a
int a = 100; Output:
//get the value, 100
cout << a <<endl; //prints 100 1024
//get the memory address
cout << &a; //prints 1024
Reference Operator (&)
■ The address that locates a variable within memory is what we call a reference to
that variable.
■ This reference to a variable can be obtained by preceding the identifier of a variable
with an ampersand sign (&), known as reference operator,
■ The literal meaning of & is “ address of ". For example:
ted = &andy; //ted equals to address of andy
andy = 25;
fred = andy;
ted = &andy;
int a = 100;
int *p = &a;
cout << a << " " << &a <<endl;
cout << p << " " << &p <<endl;
■ The concept of array is very much bound to the one of pointer. The identifier of an array
is equivalent to the address of its first element, supposing these two declarations:
■ The following assignment operation would be valid:
int numbers [20];
int * p;
■ p and numbers would be equivalent and would have the same properties.
■ Unlike p, which is an ordinary pointer, numbers is an array, and an array can be
considered a constant pointer. Therefore, the following allocation would not be valid:
p = numbers; correct
■ Because numbers is an array, so it operates as a constant pointer, and we cannot assign
values to constants.
numbers = p; wrong
Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 13
Check your understanding ??
Predict output ??
■ When declaring pointers we may want to explicitly specify which variable we want them to point to:
char *terry = “hello”;
■ It is important to indicate that terry contains the value 1702, and not 'h' nor "hello", although 1702
indeed is the address of both of these.
■ Check point ??
output ??
cout << *(terry +4);
cout << terry [4];
a 2 p - 1
a + 1 4 p
a + 2 6 p + 1
a + 3 8 p + 2
a + 4 22 p + 3
10.5. Pointer to Pointer
58 58 58
10.6. Pointer and Functions
void IndirectSwap(char *Ptr1, char *Ptr2)
{
char temp = *Ptr1;
*Ptr1 = *Ptr2;
*Ptr2 = temp;
}
int main()
{
char a = 'y';
char b = 'n';
IndirectSwap(&a, &b);
cout << a << b << endl;
return 0;
}
All the studying
you are doing will
be worth it in the End !!
Good Luck