Lec 9 Pointers
Lec 9 Pointers
Pointers
Introduction
• 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.
• Its declared as type *var-name;
– Type is the pointer's base type; it must be a valid C data type
– Var-name is the name of the pointer variable
– The asterisk * used to declare a pointer
Examples
• int *ip; /* pointer to an integer */
• double *dp; /* pointer to a double */
• float *fp; /* pointer to a float */
• char *ch /* pointer to a character */
Pointers and addresses
• a pointer is a variable whose value is a memory address
• int count = 10;
• int *int_pointer;
• int_pointer = &count;
• The address operator has the effect of assigning to the variable
int_pointer, not the value of count, but a pointer to the variable count.
• We say that int_ptr "points to" count
• The values and the format of the numbers representing memory addresses
depend on the computer architecture and operating system. In order to
have a portable way of representing memory addresses, we need a
different type than integer !
• To print addresses: %p
Indirection (dereferencing) operator *
• To reference the contents of count through the pointer variable
int_pointer, you use the indirection operator, which is the asterisk *
as an unary prefix operator. *int_pointer
• If a pointer variable p has the type t*, then the expression *p has
the type t
// Program to illustrate pointers
#include <stdio.h>
int main (void)
{
int count = 10, x;
int *int_pointer;
int_pointer = &count;
x = *int_pointer;
printf ("count = %i, x = %i\n", count, x);
return 0;
}
Example: pointers
// Program to illustrate pointers
#include <stdio.h>
int main (void)
{
int count = 10;
int *ip;
ip = &count;
printf ("count = %i, *ip = %i\n", count, *ip);
*ip=4;
printf ("count = %i, *ip = %i\n", count, *ip);
return 0;
}
Application example of Pointers
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* 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: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Using pointer variables
• The value of a pointer in C is meaningless until it is set pointing to
something !
int *p; Severe runtime error !!! the value 4 is stored in the location to
which p points. But p, being uninitialized, has a random value,
*p = 4; so we cannot know where the 4 will be stored !
• How to set pointer values:
– Using the address operator
int *p;
int x;
p = &x;
*p = 4;
– Using directly assignements between pointer variables
int *p;
int *p1;
int x;
p1 = &x;
p = p1;
*p = 4;
NULL pointers
• Values of a pointer variable:
– Usually the value of a pointer variable is a pointer to some other variable
– Another value a pointer may have: it may be set to a null pointer
• A null pointer is a special pointer value that is known not to point anywhere.
• No other valid pointer, to any other variable, will ever compare equal to a
null pointer !
• Predefined constant NULL, defined in <stdio.h>
• Good practice: test for a null pointer before inspecting the value pointed !
#include <stdio.h>