0% found this document useful (0 votes)
54 views

C - Pointer

Pointer is a variable that stores the address of another variable. There are several important pointer concepts in C: 1) Declaring a pointer variable to store the address of another variable. 2) Dereferencing a pointer using * operator to access the value at the stored address. 3) Pointer arithmetic allows incrementing/decrementing a pointer to point to next/previous memory locations. 4) Pointers allow passing addresses to functions and returning addresses from functions.

Uploaded by

Sadman Wasif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

C - Pointer

Pointer is a variable that stores the address of another variable. There are several important pointer concepts in C: 1) Declaring a pointer variable to store the address of another variable. 2) Dereferencing a pointer using * operator to access the value at the stored address. 3) Pointer arithmetic allows incrementing/decrementing a pointer to point to next/previous memory locations. 4) Pointers allow passing addresses to functions and returning addresses from functions.

Uploaded by

Sadman Wasif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

C – Pointer

C – Pointer
If you want to be proficient in the writing of code in the C
programming language, you must have a thorough
working knowledge of how to use pointers.
Some C programming tasks are performed more easily
with pointers, and other tasks, such as dynamic memory
allocation, cannot be performed without using pointers.
So it becomes necessary to learn pointers to become a
perfect C programmer.
As you know, every variable is a memory location and
every memory location has its address defined which can
be accessed using ampersand (&) operator, which
denotes an address in memory. Consider the following
example, which will print the address of the variables
defined:
C – Pointer

So you understood what is memory address and how


to access it, so base of the concept is over. Now let us
see what is a pointer.
What is Pointer?
A pointer is a variable whose value is the address of
another variable, i.e., direct address of the memory
location. Like any variable or constant, you must declare a
pointer before you can use it to store any variable address.
The general form of a pointer variable declaration is:

dataType *var_name;
Here,
 dataType is the pointer's base type; it must be a valid C data
type(i.e., int, float, char etc).
 var_name is the name of the pointer variable.
 The asterisk * you used to declare a pointer is the same
asterisk that you use for multiplication. However, in this
statement the asterisk is being used to designate a variable as
a pointer.
Pointer
Following are the valid pointer declaration:

 The actual data type of the value of all pointers, whether


integer, float, character, or otherwise, is the same, a long
hexadecimal number that represents a memory address.
 The only difference between pointers of different data
types is the data type of the variable or constant that the
pointer points to.
How to use Pointers?
There are few important operations, which we will do with
the help of pointers very frequently.
① we define a pointer variable
② assign the address of a variable to a pointer and
③ finally access the value at the address available in the
pointer variable by dereferencing.
Dereferencing is done by using unary operator * that
returns the value of the variable located at the address
specified by its operand.
Dereferencing a pointer means getting the value stored in
the memory at the address which the pointer “points” to.
The * is the value-at-address operator, also called the
indirection operator. It is used both when declaring a
pointer and when dereferencing a pointer.
How to use Pointers?
Following example makes use of these operations:

 When the above code is compiled and executed, it produces result


something as follows:
How to use Pointers?
the & is the address-of operator and is used to
reference the memory address of a variable.
By using the & operator in front of a variable
name we can retrieve the memory address-of
that variable. It is best to read this operator as
address-of operator.
Following code shows some common notations
for the value-at-address (*) and adress-of (&)
operators.
NULL Pointers in C
 It is always a good practice to assign a NULL value to a
pointer variable in case you do not have exact address to be
assigned. This is done at the time of variable declaration. A
pointer that is assigned NULL is called a null pointer.
 The NULL pointer is a constant with a value of zero defined in
several standard libraries. Consider the following program:

 When the above code is compiled and executed, it


produces the following result:
NULL Pointers in C
 On most of the operating systems, programs are not
permitted to access memory at address 0 because that
memory is reserved by the operating system.
However, the memory address 0 has special
significance; it signals that the pointer is not intended
to point to an accessible memory location. But by
convention, if a pointer contains the null (zero) value,
it is assumed to point to nothing.
 To check for a null pointer you can use an if statement
as follows:
C Pointers in Detail
 Pointers have many but easy concepts and they are very
important to C programming. There are following few important
pointer concepts which should be clear to a C programmer:
C - Pointer arithmetic
C pointer is an address, which is a numeric value. Therefore, you
can perform arithmetic operations on a pointer just as you can a
numeric value.
There are four arithmetic operators that can be used on pointers:
++, --, +, and -
To understand pointer arithmetic, let us consider that ptr is an
integer pointer which points to the address 1000. Assuming 32-
bit integers, let us perform the following arithmetic operation on
the pointer:

++ptr

Now, after the above operation, the ptr will point to the location
1004 because each time ptr is incremented, it will point to the
next integer location which is 4 bytes next to the current location.
If ptr points to a character whose address is 1000, then above
operation will point to the location 1001 because next character
will be available at 1001.
Incrementing a Pointer
We prefer using a pointer in our program instead of an array
because the variable pointer can be incremented, unlike the
array name which cannot be incremented because it is a
constant pointer.
The following program increments the variable pointer to
access each succeeding element of the array (see next slide).
Incrementing a Pointer
Decrementing a Pointer
The same considerations apply to decrementing a
pointer, which decreases its value by the number of
bytes of its data type as shown below:
Pointer * and ++
Pointer Comparisons
Pointers may be compared by using relational
operators, such as ==, <, and >.
If p1 and p2 point to variables that are related to
each other, such as elements of the same array,
then p1 and p2 can be meaningfully compared.
The following program modifies the previous
example one by incrementing the variable
pointer as long as the address to which it points
is either less than or equal to the address of the
last element of the array, which is &var[MAX - 1]
(see next slide).
Pointer Comparisons
Array of pointers
Before we understand the concept of arrays of
pointers, let us consider the following example,
which makes use of an array of 3 integers:
Array of pointers

There may be a situation when we want to


maintain an array, which can store pointers to
an int or char or any other data type available.
Following is the declaration of an array of
pointers to an integer:

int *ptr[ ];

This declares ptr as an array of integer


pointers. Thus, each element in ptr, now holds
a pointer to an int value. Following example
makes use of three integers, which will be
stored in an array of pointers as follows:
Array of pointers
Following example makes use of three integers,
which will be stored in an array of pointers as
follows:
Array of pointers
You can also use an array of pointers to character to
store a list of strings as follows:
Pointer to Pointer
A pointer to a pointer is a form of multiple indirection, or
a chain of pointers. Normally, a pointer contains the
address of a variable. When we define a pointer to a
pointer, the first pointer contains the address of the
second pointer, which points to the location that
contains the actual value as shown below.

A variable that is a pointer to a pointer must be declared


as such. This is done by placing an additional asterisk in
front of its name. For example, following is the
declaration to declare a pointer to a pointer of type int:
Pointer to Pointer
When a target value is indirectly pointed to by a pointer to a
pointer, accessing that value requires that the asterisk operator
be applied twice, as is shown below in the example:
Passing pointers to functions
C programming language allows you to pass a pointer to a function.
To do so, simply declare the function parameter as a pointer type.
Following is a simple example where we pass an unsigned long
pointer to a function and change the value inside the function which
reflects back in the calling function:
Passing pointers to functions
The function, which can
accept a pointer, can also
accept an array as shown
in the following example:
Return pointer from functions
C allows us to return a pointer from a function. To do
so, you would have to declare a function returning a
pointer as in the following example:

Second point to remember is that, it is not good


idea to return the address of a local variable to
outside of the function so you would have to define
the local variable as static variable.
Return pointer from functions
Consider the following
function which will generate
10 random numbers and
return them using an array
name which represents a
pointer i.e., address of first
array element.

You might also like