0% found this document useful (0 votes)
5 views10 pages

Lec 9 Pointers

Uploaded by

dennismwenda342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Lec 9 Pointers

Uploaded by

dennismwenda342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lecture 9

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>

int *ip = NULL;

if(ip != NULL) printf("%d\n", *ip);

if(ip ) printf("%d\n", *ip);


const and pointers
• With pointers, there are two things to consider:
– whether the pointer will be changed
– whether the value that the pointer points to will be changed.
• Assume the following declarations:
char c = 'X';
char *charPtr = &c;
• If the pointer variable is always set pointing to c, it can be declared as a
const pointer as follows:
char * const charPtr = &c;
*charPtr = 'Y'; // this is valid
charPtr = &d; // not valid !!!
• If the location pointed to by charPtr will not change through the pointer
variable charPtr, that can be noted with a declaration as follows:
const char *charPtr = &c;
charPtr = &d; // this is valid
*charPtr = 'Y'; // not valid !!!
Uses of Pointers

You might also like