- A pointer is a memory address of a variable.
- Suppose the memory
location of variable x is 1004. Then the address of x is 1004.
Or 1004 is the pointer for x. Let this be stored in another variable
y.
- Just as any other value, a pointer can also be stored. Thus
y (whose value is 1004)
can be stored in another memory location (say, 1001).
- A pointer variable is represented as *y where y is a pointer to
x. *x returns the value of x. That is, if 10 is stored in
variable x, then *y returns 10.
- &x returns the address of x, i.e., the contents of y.
- Some programs execute more efficiently with pointers and
some routines can be performed only using pointers.
There are two operators to deal with pointers:
- The & ( for example &x) is a unary operator that
returns the memory address of its operand ( x).
- The * (for example *y) is a unary operator that returns
the value of the variable located at the address of
the value stored in y.
- *y and x both refer to the contents of x
- y and &x refer to the address of x
- accessing the contents of a variable by using the variable name
is called direct access.
- accessing the contents of a variable by using a pointer to the
variable is called indirect access.
- A pointer must be declared before it can be used.
typename *ptrname;
float *a;
int *b;
char *c; - A pointer must be initialized before using.
pointer = &variable;
a = &d;
b = &e;
c = &f;
#include <stdio.h>
int main()
{
/* Declare and initialize an int variable */
int var = 1;
/* Declare a pointer to int */
int *ptr;
/* Initialize ptr to point to var */
ptr = &var;
/* Access var directly and indirectly */
printf("\nDirect access, var = %d", var);
printf("\nIndirect access, var = %d", *ptr);
/* Display the address of var two ways */
printf("\n\nThe address of var = %d", &var);
printf("\nThe address of var = %d", ptr);
}
- The number of bytes occupied by different variable types varies
- When a pointer is used to handle addresses of a multibyte
variable, the address of the variable is the address of the lowest
byte it occupies.
Declaring and initializing variables
int vi = 122;
char vc = 'a';
float vf = 12.4;
Declaring and initializing pointers
int *p_vi;
char *p_vc;
float *p_vf;
.............
p_vi = &vi;
p_ch = &vc;
p_vf = &vf;
- when we use an array, we are in fact using pointers without
knowing it
- the name of the array itself is a pointer
- if we use an array expenses[13], expenses is the address of the
first element in the array (i.e.,
&expenses[0]
int array[100], *p_array;
p_array = array;
- To access successive elements in an array, a pointer must be
increased by sizeof(datatype)
- incrementing (decrementing) pointers - ptr++ (ptr-)
- ptr++ increments the value of ptr based on the data type. If
integer data type is stored in the array, ptr++ automatically
increments the ptr by 2
- the statement ptr += 4 increases the value stored in ptr by 8
if the array stores integer values, thus the ptr actually points to
four array elements ahead
/* Demonstrates using pointer arithmetic to access */
/* array elements with pointer notation. */
#include <stdio.h>
#define MAX 10
int main()
{
/* Declare and initialize an integer array. */
int i_array[MAX] = { 0,1,2,3,4,5,6,7,8,9 };
/* Declare a pointer to int and an int variable. */
int *i_ptr, count;
/* Declare and initialize a float array. */
float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, 9 };
/* Declare a pointer to float. */
float *f_ptr;
/* Initialize the pointers. */
i_ptr = i_array;
f_ptr = f_array;
/* Print the array elements. */
for (count = 0; count < MAX; count++)
printf("\n%d\t%f", *i_ptr++, *f_ptr++);
return 0;
}
- While incrementing and decrementing, the C compiler does not keep track of the beginning and ending of the array. It is the responsibility of the programmer. The program can increase or decrease the pointer so that it points some where in the memory before or after the array.
- lower array elements (with smaller subscripts) always have a lower address than higher array elements (larger subscripts)
*(array) == array[0]
*(array 1) == array[1]+
......
*(arrayn) == array[n]+ - assignment - it assigns a value to a pointer. This value should be an address obtained with the address-of operator (&) or from the pointer constant (array name)
- A pointer should always be initialized. Otherwise,
int *ptr;
*ptr = 12;
Here ptr has not been initialized. So this statement places the value 12 in memory
location pointed by ptr . - indirection - the indirection operator (*) gives the value stored in the point-of location.
- address-of -- Used to find the address of a pointer so that we can have pointers to pointers
- ptr1 -ptr2 subtracts two pointers (called differencing). Two pointers to different elements to the same array can be subtracted. This can be used to find how far apart the elements are.
- ptr comparisons:
==, !=, >, <, >=, <= They work only if pointers refer to the same array - multiplication, division, and modulus do not make sense with pointers. If used, produce errors
- An argument is a value that a calling program passes to a function. The argument must be a single numerical value, but cannot be an array.
- The only way an array can be passed to a function is by means of a pointer
- Thus, to pass an array to a function, we send its address, which is a single numerical value.
- The function then knows the address of the array and can then access all the elements in an array.
- To let the function know the size of the array,
-
the programmer identifies the last element of the array by storing a special value in there
- pass the array size as another argument
/* Passing an array to a function. */
#include <stdio.h>
#define MAX 10
int largest(int x[], int);
int main()
{
int array[MAX], count;
/* Input MAX values from the keyboard. */
for (count = 0; count < MAX; count++)
{
printf("Enter an integer value: ");
scanf("%d", &array[count]);
}
/* Call the function and display the return value. */
printf("\n\nLargest value = %d", largest(array, MAX));
return 0;
}
/* Function largest() returns the largest value */
/* in an integer array */
int largest(int x[], int y)
{
int count, biggest = -12000;
for ( count = 0; count < y; count++)
{
if (x[count] > biggest)
biggest = x[count];
}
return biggest;
}
/* Passing an array to a function. Alternative way. */
#include <stdio.h>
#define MAX 10
int largest(int x[]);
int main()
{
int array[MAX+1], count;
/* Input MAX values from the keyboard. */
for (count = 0; count < MAX; count++)
{
printf("Enter an integer value: ");
scanf("%d", &array[count]);
if ( array[count] == 0 )
count = MAX; /* will exit for loop */
}
array[MAX] = 0;
/* Call the function and display the return value. */
printf("\n\nLargest value = %d", largest(array));
return 0;
}
/* Function largest() returns the largest value */
/* in an integer array */
int largest(int x[])
{
int count, biggest = -12000;
for ( count = 0; x[count] != 0; count++)
{
if (x[count] > biggest)
biggest = x[count];
}
return biggest;
}
- Strings are stored in arrays of type char, with teh end of the
string marked with a null character.
- The name of the string is the pointer.
- To pass a string to the several library functions that
manipulate strings, the programmer has to just pass the name of the string
This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html -split 0 -html_version 3.0 c5.tex.
The translation was initiated by Vijay & on Thu Feb 19 11:48:15 EST 1998
Vijay &
Thu Feb 19 11:48:15 EST 1998