Pointer - Pointers Outline Pointer variables More About Pointer Operators More About Pointer Variable Declarations The Size of Pointer Variables

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Pointers Outline Pointer variables More About Pointer Operators More About Pointer Variable Declarations The Size of Pointer Variables Pointer Arithmetic Using Pointer for Hardware Interface Calling Functions by Reference Using Pointers Relationship Between Pointers and Arrays Dynamic Allocation of Memory Functions Return Pointers Pointer to Pointers Arrays of Pointers Using Pointers to Pass One-Dimensional Arrays to a Function Pointers Outline Pointer to arrays Dynamic Memory Allocation of Arrays Using Pointers to Pass Multi-Dimensional Arrays to a Function Pointer to Functions Pointer variables – A pointer is defined as a variable which contains the address of another variable or dynamically allocated memory. – With operator ‘*’ in front of the variable names, the variables of pointer type can be declared similar to variables of other data types. The unary operator ‘&’ gives the “address of a variable”. Example : int i, *p; i = 10; p = &i; // p now stores the address of i p = &i; // p now stores the address of I If the address of i is 0x00E81E20, the value of p is 0x00E81E20 as shown below. i p More About Pointer Operators • & (address operator) – To obtain the address of a variable, add the address operator ‘&’ in front of the variable name. int i = 10; int *iPtr; iPtr = &i; //iPtr gets address of i iPtr <==> &i • * (indirection/dereferencing operator) – To declare a variable of pointer, add the operator ‘*’ in front of the identifier. – To obtain the value of a variable, add the pointer operator ‘*’ in front of a pointer’s variable name. *iPtr <==> i *iptr gives the value of i ( iptr points to i ) – * can be used for assignment *iptr = 5; // changes i to 5 Example: /* File: poperators.c */ #include <stdio.h> int main() { int i, *p; i = 10; p = &i; /* p <==> &I, *p <==> I */ printf("The value of i is %d\n", i); printf("The value of *p is %d\n\n", *p); printf("The value of &i is %p\n", &i); printf("The value of p is %p\n\n", p); return 0; } Output: The value of i is 10 The value of *p is 10 The value of &i is 00E81E20 The value of p is 00E81E20 More About Pointer Variable Declarations – Using following format to declare a pointer variable. type *pointer_name; For example, following statement declares a pointer to an int int *iPtr; – Can declare pointers to any data type such as int, float, double, and complex, etc. – Without initialization, the value of declared pointers is or NULL. • or NULL pointer points to nothing ( NULL preferred) – Pointer to void ( void * ) • Generic pointer, represents any type. void pointers cannot be dereferenced void *p; /* *p is invalid */ The asterisk in declaration is not a deference operator even for declaration with initialization. For example, > int i > int *p = &i // initialization > i = 10 10 > *p 10 The Size of Pointer Variables The varaible of pointer type is used to hold an address of the memory....
View Full Document

This note was uploaded on 04/16/2008 for the course MATH 21A taught by Professor Osserman during the Fall '07 term at UC Davis.

Page1 / 59

Pointer - Pointers Outline Pointer variables More About Pointer Operators More About Pointer Variable Declarations The Size of Pointer Variables

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online