Pointer Tutorial in Class

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Pointers Tutorial in class

What Are Pointers?


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:
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 * 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. Following are the valid pointer declaration:
int
double
float
char

*ip;
*dp;
*fp;
*ch

/*
/*
/*
/*

pointer
pointer
pointer
pointer

to
to
to
to

an integer */
a double */
a float */
a character */

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.
(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.
Following example makes use of these operations:
// Program 1.
#include <stdio.h>
int main ()
{
int var = 20;
int *ip;

/* actual variable declaration */


/* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/


printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %p\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;

When the above code is compiled and executed, it produces result something as
follows:
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20

Complete program 1 and compare output with proposed above. Did you receive the same
results? Why?
Example to demonstrate working of pointers

Complete program 2 and compare output with proposed above. Did you
receive the same results? Why?
// Program 2.

/* Source code to demonstrate, handling of pointers in C program */


#include <stdio.h>
int main(){
int* pc;
int c;
c=22;
printf("Address of c:%d\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%d\n",&c);
printf("Value of c:%d\n\n",c);
return 0;
}

Output:

Address of c: 2686784
Value of c: 22
Address of pointer pc: 2686784
Content of pointer pc: 22
Address of pointer pc: 2686784
Content of pointer pc: 11
Address of c: 2686784
Value of c: 2

Explanation of program and figure


1. Code int* pc; creates a pointer pc and a code int c; creates normal variable c. Pointer
pc points to some address and that address has garbage value. Similarly, variable c also
has garbage value at this point.
2. Code c=22; makes the value of c equal to 22, i.e.,22 is stored in the memory location of
variable c.
3. Code pc=&c; makes pointer, point to address of c. Note that, &c is the address of variable
c (because c is normal variable) and pc is the address of pc (because pc is the pointer
variable). Since the address of pc and address of c is same, *pc will be equal to the value
of c.
4. Code c=11; makes the value of c, 11. Since, pointer pc is pointing to address of c. Value
inside address pc will also be 11.
5. Code *pc=2; change the contents of the memory location pointed by pointer pc to change
to 2. Since address of pointer pc is same as address of c, value of c also changes to 2.
// Program 3
// 1. Declare a pointer to an integer called address.
// 2. Assign the address of a integer variable balance to the pointer address.
// 3. Assign the character value 'W' to the variable pointed to by the char pointer letter.
// 4. Output value of balance and the character value on to the screen by using dereferencing
pointers.
// Program 4
// What is the output of the following program segment?
int

count = 10, *temp, sum = 0;

temp = &count;
*temp = 20;
temp = &sum;
*temp = count;
printf("count = %d, *temp = %d, sum = %d\n", count, *temp, sum );

// Program 5
The string The day is done is stored somewhere in memory, and its possible to
increment and decrement pointer variable to access the string individual
characters:
char *verse = The day is done;
So, in this case:
verse - assign to address of the character T;
verse +1 - assign to address of the character h;

verse + 2 - assign to address of the character e;


etc
Print the characters of the string pointed at by verse, one per line.

You might also like