Sie sind auf Seite 1von 19

Pointer

POINTER
•Pointer = a variable that contains memory address of
another variable as its value

•points to a specific location in memory.

• a pointer represents the address and NOT the value


of a variable.

•The pointer variable can be declared by the following


syntax
Fundamentals
• v is a variable that represents some
particular item. The compiler will
automatically assign memory cells or this
data item.
• The data item can then be accessed if we
know the location (an address) of the first
memory cell.
• the address of v’s memory location can
be determined by the expression &v,
where & is a unary operator, call the
address operator, that evaluates the
address of its operand.
Fundamentals
Now let us assign the address of v to
another variable, pv. Thus:

pv = &v

•This new variable is called a pointer to v


since it “points” to the location where v is
stored in memory.
•Remember, however that pv represents
v’s address, not it’s value.
•Thus, pv is referred to as a pointer
variable.
Fundamentals
The relationship between pv and v is illustrated in
figure below

Address of v Value of v
pv v
Relationship between pv and v (where pv = &v and v = *p

Data item represented by v can be access by the


expression *pv, where * is a unary operator, called
indirection operator, that operates only on a
pointer variable. Therefore *pv and v both
represent the same data item.
Declaring a Pointer
•example:
int *room;
room, letter
char *letter; and pi are pointers
float *pi;
*room, *letter
and *pi are referred to
as pointers’
values
#include <stdio.h>
/* Assign the add of &suite = 12345*/
/* Add of room = 12367 */

void main()
{
int *room; /* room is a pointer */
int suite=121; /* suite number is 121 */
room=&suite; /* room points to the address
of suite */

/* Display the value of the pointer room by


using ‘*’ */
printf(“\nthe room number is %d\n”, *room);
}
Pointer Operators
Using the address operator (&) to
indicate the address of its operand

int *room; Assigns the address


int suite=121; of variable suite
room=&suite; to pointer variable
room

It is said that room pointer points


to suite
Pointer Operators – cont.
The asterisk (*) is said to return
the value of object to which the
pointer points to room pointer
contains the value
int *room; pointed by it
i.e. value of
int suite=121,suite2=100;
room=&suite; suite
room=&suite2;

printf(“%d”, *room);
Tips to remember
int *room;

- room is the name of pointer (stores address of


another variable i.e. suite)

- *room is the value found at the address pointed by


pointer room
Passing Pointers to a
Function
Pointers and function:
The pointer are very much used in a function
declaration. Sometimes only with a pointer a
complex function can be easily represented and
success. The usage of the pointers in a function
definition may be classified into two groups.

1. Call by reference (address or location)


2. Call by value.
Pointers and Function
When an argument is passed by value, the
data item is copied to the function. Thus,
any alteration made to the data item within
the function is not carried over into the
calling routine.
When an argument is passed by
reference, however the address of a data
item is passed to the function. The content
of that address can be accessed freely,
either within the function or within the
calling routine.
Pointers and Array
An array is actually very much like pointer.
We can declare the arrays first element as a[0] or as
int *a because a[0] is an address and *a is also an
address the form of declaration is equivalent.
The difference is pointer is a variable and can appear
on the left of the assignment operator that is value.
The array name is constant and cannot appear as the
left side of assignment operator.
Dynamic Memory
Allocation
Dynamically allocated variable is a type of variable that doe
ot exist when the program is loaded but it is dynamically
reated when needed

You can use it as many as you like and later on deallocate


when it is not needed

By pointing it to a defined structure, you may have a progra


WITHOUT any variables created

reserving memory only when you need is called DMA.


Dynamic Variable Creation
Creating a pointer will ease up the use
of variables defined in a structure
This pointer is created dynamically and
later deallocated from the memory
By having malloc() function, it allows a
piece of memory on a heap to be
allocated for data and variable
Deallocating data and variables will
cause holes which are then made
available to other additional variables
Heap
An area in your memory accessible
to store variables
Using malloc() will reserve certain
portion of the heap to the dynamic
variables declared
These variables can be deallocated
later on by using free()
malloc() and free()
function
malloc() allocates memory at the size
you want
void *malloc (size_t size);
 size = memory size
 returns the allocated address
free() will free the memory reserved
from malloc()
void free (void *ptr);
 ptr = memory address to be freed
Exercise
Modify the program that illustrate the
relationship between two integer variables as
follows:
 Use floating-point data rather than integer

data. Assign an initial value of 0.3 to u.


 Use double-precision data rather than
integer data. Assign an initial value of 0.3 x
1045 to u.
 Use character data rather than integer data.

Assign an initial value of ‘C’ to u.


Be sure to modify the printf statement
accordingly
Exercise
Modify the program example from passing
pointers to a function, so that a single one-
dimensional, character-type array is passed to
funct1. Delete funct2 and all references to
funct2. Initially, assign the string “red” to the
array within main. Then reassign the string
“green” to the array within funct1. Execute the
program and compare the results. Remember
to modify the printf statements accordingly.

Das könnte Ihnen auch gefallen