Pointers - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Pointers

Description:

Each person has a name and an address and belongings! ... A pointer MUST be initialized by assigning the address of a valid variable to it. ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 15
Provided by: gae5
Category:
Tags: address | pointers

less

Transcript and Presenter's Notes

Title: Pointers


1
Pointers
2
Variables and Pointers are like People
  • Each person has a name and an address and
    belongings!
  • A friend that depends upon you, uses your
    address, and has access to your belongings, is a
    pointer!

3
Declaring and Initializing Pointers
  • int num 45 base integer variable
  • declared and initialized
  • int num_ptr pointer to an integer
  • declared ( still pointing to garbage)
  • num_ptr num pointer to an integr
  • initialized to a valid address

4
What do Pointers Point To?
  • A newly declared pointer points to garbage.
  • A pointer MUST be initialized by assigning the
    address of a valid variable to it. Otherwise, it
    is dangerous to use it, it could be pointing
    anywhere, we dont know where!

5
How are Pointers Different from Other Variables?
  • Each variable has a name (num1, var1)
  • Each variable has a location (address)
  • Each variable has contents
  • A pointer has a name (num1_ptr,var1_ptr)
  • A pointers contents is the address of another
    variable!

6
Pointer to integer int
  • int num 45
  • int num_ptr num
  • variable
    pointer
  • Name num num_ptr
  • Contents 45 B1021205
  • Address B1021205

7
Pointer to char char
  • char letter A variable
  • char ltr_ptr letter pointer
  • variable pointer
  • Name letter ltr_ptr
  • Contents A CF30A015
  • Address CF30A015

8
Pointer to an array datatype
  • char my_string10 My String
  • char string_ptr my_string
  • NOTE the name of an array without the subscript
    holds the address to the first position of that
    array. It is a pointer, too.

9
Pointer to struct struct
  • Struct my_tagS
  • int x, int y
  • struct my_tagS struct1 4, 5
  • struct my_tagS struct_ptr struct1

10
Pointer to struct (arrow notation)
  • struct_ptr-gtx 10
  • (arrow notation replaces dot notation when using
    a POINTER to a struct)

11
Accessing the Pointer Address (Pointer Arithmetic)
  • adds the sizeof an int to the address in the
    pointer (the pointer subsequently points to the
    next integer space in memory).
  • subtracts the sizeof an int, moves the pointer to
    the previous location in memory.
  • num_ptr
  • num_ptr 1
  • num_ptr--
  • num_ptr - 1

12
Accessing Variable Values with Pointers
(Dereferencing)
  • num_ptr holds an address
  • num_ptr accesses the contents
  • (asterisk) Dereferences a pointer
  • num_ptr 3 Wrong - this tries to assign an
    address location of 3 to the pointer.
  • num_ptr 3 Correct - changes the contents
    pointed to by num_ptr to 3.

13
Passing Pointers as Parameters
  • int my_int intvar pointer to be passed
  • my_func( my_int ) function call
  • void my_func ( int input_int ) new function
  • declare a pointer using the (asterisk)
  • pass the pointer without the (asterisk)
  • receive the pointer into a new variable in the
    function header using the (asterisk)

14
  • Break
  • Pointer Lab
Write a Comment
User Comments (0)
About PowerShow.com