Pointers In C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Pointers

What is pointer?
 A pointer is a variable that points at, or refers to, another variable.
 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 using it to store any variable address–
 The general form of a pointer variable declaration is - type *var-name;
 Here, type is the pointer's base type; it must be a valid C data type
and var-name is the name of the pointer variable. The asterisk *
used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being
used to designate a variable as a pointer.
 Example-
 int *ip; /* pointer to an integer */
 double *dp; /* pointer to a double */
 float *fp; /* pointer to a float */
 char *ch /* pointer to a character */

How to Use Pointers?


 There are a few important operations, which we will do with the help of
pointers very frequently.
 (a) We define a pointer variable,
 (b) assign the address of a variable to a pointer and
 (c) Finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the
variable located at the address specified by its operand.

Pointer Notation
 Consider the example given below
int i = 3;

 We see that the computer has selected memory location 65524 as the
place to store the value 3. The location number 65524 is not a number
to be relied upon, because some other time the computer may choose a
different location for storing the value 3. The important point is, i’s
address in memory is a number.
 We can take the above output using the program given below

 O/P- Address of i = 65524


Value of i = 3
 Point to remember in above program- The first printf( ) statement in
above program ‘&’ used in this statement is C’s ‘address of’ perator. The
expression &i returns the address of the variable i, which in this case
happens to be 65524.
 The other pointer operator available in C is ‘*’, called ‘value at
address’ operator. It gives the value stored at a particular address.

 Point to remember in above program- Value of *( &i ) is same as printing


the value of i and the expression &i gives the address of the variable i.

The address of i will store in variable j. We can understand the above


expression diagrammatically is give below

Now to understand the above situation in program lets take an


example.

 Here, alpha, ch and s are declared as pointer variables, i.e.,
variables capable of holding addresses.
 Pointers are variables that contain addresses, and since addresses
are always whole numbers, pointers would always contain whole
numbers.
 The declaration float *s does not mean that s is going to contain a
floating-point value. it means is, s is going to contain the address
of a floating-point value.

Call by value and call by reference


 Two types of function calls—call by value and call by reference.
Arguments can generally be passed to functions in one of the two
ways:
1. sending the values of the arguments
2. sending the addresses of the arguments
Example of call by value

Example of call by reference


From the programs that we discussed here, we can draw the following
conclusions:
(a) If we want that the value of an actual argument should not get changed in
the function being called, pass the actual argument by value.
(b) If we want that the value of an actual argument should get changed in the
function being called, pass the actual argument by reference.
(c) If a function is to be made to return more than one value at a time, then
return these values indirectly by using a call by reference.

Points to remember
(a)Pointers are variables which hold addresses of other variables.
(b) A pointer to a pointer is a variable that holds address of a pointer variable.
(c) The & operator fetches the address of the variable in memory.
(d) The * operator lets us access the value present at an address in memory
with an intension of reading it or modifying it.
(e) A function can be called either by value or by reference.
(f) Pointers can be used to make a function return more than one value
simultaneously in an indirect manner.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy