Lec 4
Lec 4
Lecture (4)
Pointers
Pointers
• A pointer is a variable that contains the memory location of another variable.
• Pointers are used to pass information back and forth between functions.
• Pointers enable the programmers to return multiple data items from a function
via function arguments.
• Pointers are used in data structures such as arrays.
• The general syntax of declaring pointer variables is:
Pointers
Pointers
• data_type is the data type of the value that the pointer will point to.
• & is called the address operator, and * is called the dereference operator.
Example
Example
sum = 5
mul = 10
*ptr2 = 4
div = -21
• The expression *ptr++ is equivalent to
*(ptr++) and will increase the value of ptr so
that it now points to the next memory
location.
• To increment the value of the variable whose
Note
address is stored in ptr, write (*ptr)++
Example
1 3
2
Example
1 3
2 4
Example