day15 - Pointers 1 What is a pointer? pointer: A type...

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

View Full Document Right Arrow Icon
    1 Pointers
Background image of page 1

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

View Full DocumentRight Arrow Icon
    2 What is a pointer? pointer : A type representing an address to another value elsewhere in the computer Pointer declaration, syntax: <type> * <name> ; Example: int *pNumber; // pointer to an int double *pAnother; // pointer to a double Naming convention: Pointer variable names begin with a lowercase p .
Background image of page 2
    3 Assigning values to pointers To get the address of another variable, you have to use the address of operator (&). Example: double number = 12.3; double *pNumber = &number;
Background image of page 3

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

View Full DocumentRight Arrow Icon
    4 Pointers illustration
Background image of page 4
    5 Assigning values to pointers Pointers can only point to variables of one type. Example: int integer = 12; double real = 34.5; int *pInteger = &integer; // ok int *pInteger = &real; // error! double *pReal = &real; // ok
Background image of page 5

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

View Full DocumentRight Arrow Icon
    6 Accessing the value pointed to  To read or write the value pointed to by the pointer, you use the dereference operator (*) Example: int number = 10; int *pNumber = &number; *pNumber = 12; // number is now 12 printf("%d\n", *pNumber + 2); // 14 printf("%d\n", number + 2); // 14
Background image of page 6
    7 Why use pointers?
Background image of page 7

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

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

Page1 / 16

day15 - Pointers 1 What is a pointer? pointer: A type...

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

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