Advanced Variables: Pointers - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Advanced Variables: Pointers

Description:

NOTE: The dereferencing operator is not used when assigning the ... To manipulate the value of the variable being pointed to, use the dereferencing operator: ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 26
Provided by: Dua47
Category:

less

Transcript and Presenter's Notes

Title: Advanced Variables: Pointers


1
Advanced Variables Pointers
2
Pointers
  • Pointer variables
  • Derived data type data type built from one of
    the standard types.
  • A special kind of variable that points to
    another variable (specifically it stores another
    variables memory address).
  • Declared just like other variables with one
    additional operator ( the dereferencing
    operator).
  • variable_type variable_name initial value

3
Pointers
Address Memory
0 1 2 XX MB
4
Pointers
Address Memory Variable
  • Non-pointer variables

int i10 //4B char cA //1B float f1.234
//4B
50100 50101 50102 50103 50104 50105 50106 5010
7 50108 . . .
i c f
5
Pointer Variables
  • /
  • File address.c
  • Author Duane Birnbaum
  • Project Sample C Program
  • Description This program prints the memory
    address of two variables.

  • /
  • include ltstdio.hgt
  • int main(void)
  • char a
  • char b
  • printf("p\np\n\n",a,b)
  • return 0
  • Output on MS VSC 6.0 Output on UNIX gcc
    compiler
  • 0012FF7C ffbefc4f

6
Pointers
  • Pointer variables
  • Example declarations
  • int x
  • int x
  • float y
  • myDataType myVar

7
Pointers
  • Pointer variables
  • Hold the address of what they point to.
  • Access the address of a variable using the
    address of operator
  • float ptr /declare pointer to a float/
  • ptrf /assign address of variable f to
    pointer./
  • /50103 on previous slide/
  • NOTE The dereferencing operator is not used when
    assigning the pointer.

8
Pointers
  • Pointer variables
  • To manipulate the value of the variable being
    pointed to, use the dereferencing operator
  • float f1.234
  • float ptr
  • ptrf
  • ptr12.34 /must use to avoid error/
  • printf("f\n",f)
  • printf(f\n,ptr) /same/

9
Pointers
  • Sending pointers to functions
  • Similar to sending an array
  • int bar100
  • int ptrbar
  • foo(ptr) //do not use
  • //prototype for function foo()
  • void foo(int arg) //Same as pass by ref. or
  • //passing an array.

10
Pointers
  • Sending pointers to functions
  • Treat the passed pointer variable as you would a
    pointer declared inside the function.
  • void foo(int arg)
  • int temp50
  • argtemp /changes bar from 100 to 50

11
Pointers
  • Pointer arithmetic
  • You can add and subtract integers to/from
    pointers as well as increment/decrement pointers.
    You can also subtract pointers from each other.
  • Addition/increment increases the address of the
    pointer.
  • Subtraction/decrement decreases the address of
    the pointer.
  • The amount of the increase/decrease depends on
    the number of bytes needed for the pointers base
    type (e.g. 1 for char, 4 for int, 4 for float)

12
  • includeltstdio.hgt
  • void main(void)
  • char lname"Birnbaum"
  • char ptrlname //Do not use with arrays
  • int i
  • for(i0 iltsizeof(lname)-1 i)
  • putchar(ptr)
  • Note since array variable names are pointers,
    you could also use putchar((lname i)) and
    avoid using the pointer variable.

13
Pointers
  • Pointer Comparisons
  • Relational operators may be used to compare
    pointers.
  • Example
  • Given two pointers p and q
  • if(pltq)
  • puts(p points to lower memory.\n)
  • Typically used when two pointers point to the
    same (or similar) objects (linked lists, stacks,
    queues).

14
Pointers
  • Pointers and arrays
  • They may look different but they are treated the
    same in C.
  • The name of an array is a pointer to the first
    item in the array. Other items follow in
    contiguous memory locations.
  • int scores92,85,77,91,56

15
Pointers
  • Pointers and arrays
  • You may use pointers instead of subscripts to
    access array elements.
  • scores //92
  • (scores1) //85
  • (scores2) //77
  • etc.
  • NOTE accessing arrays as pointers is not any
    more efficient than using subscripts. Use what
    you are more comfortable with.

16
Pointers
  • Pointers and arrays
  • Use pointers to assign string literals to a
    variable.
  • char lnameBirnbaum //old method
  • char lnameP
  • lnamePBirnbaum //using a pointer
  • lname is a pointer constant
  • lnameP is a pointer variable

17
Pointers
  • includeltstdio.hgt
  • void main(void)
  • char ptr0 //Stand-alone character pointer.
  • ptr0"ptr0 points to this string."
  • puts(ptr0)
  • ptr0"A shorter string."
  • puts(ptr0)
  • ptr0"A new string for ptr0 that is longer than
    the previous."
  • puts(ptr0)
  • This example runs without error. However, you
    cannot use a functions like strcpy() or gets().

18
Pointers
  • To use a character pointer inside character array
    functions, the easiest way is to reserve a
    character array and assign a character pointer to
    the first element of that array
  • void main(void)
  • char myArray101
  • char ptrmyArray //Points to 101 bytes
    including \0
  • gets(ptr) //safe as long as string is 100B or
    less
  • puts(ptr)

19
Pointers
  • Arrays of pointers
  • Arrays may hold data of any type, including
    pointers.
  • Each element in an array of pointers holds a
    pointer to a memory location.
  • The data being pointed to must be of the same
    type as the arrays base type.
  • int ptrArray10 //array of pointers to
    integers
  • char lnames10 //array of pointers to strings

20
Pointers
  • Arrays of pointers
  • Often used to hold error messages
  • void syntax_error(int errorNum)
  • static char err
  • Cannot open file\n,
  • Read error\n,
  • Write Error\n,
  • Media Failure\n
  • printf(s, errerrorNum)

21
Pointers
  • Multiple Indirection
  • Pointers that point to pointers.

Pointer
Pointer
Variable
address
address
value
  • Use an extra in front of the variable name for
    each level of indirection.
  • int x10
  • int px
  • int qp
  • printf(d, q) /print the value of x/

22
Pointers
  • Pointers to functions
  • Even though functions are not variables, they do
    have a physical location in memory that can be
    assigned to a pointer.
  • A functions address is the entry point of a
    function and therefore, a function pointer be
    used to call a function.
  • The address of a function is obtained using the
    functions name without any parentheses or
    arguments.
  • Arrays of function pointers are used to simplify
    code that might otherwise require a large switch
    structure.

23
  • includeltstdio.hgt
  • includeltstring.hgt
  • includeltstdlib.hgt
  • includeltctype.hgt
  • void compare(char a, char b, int (cmp)(const
    char , const char ))
  • int numcmp(const char a, const char b)
  • void main(void)
  • char s180,s280
  • gets(s1)
  • gets(s2)
  • if(isalpha(s1))compare(s1,s2,strcmp)
  • elsecompare(s1,s2,numcmp)
  • void compare(char a, char b, int (cmp)(const
    char , const char ))
  • printf("\nTesting for equality\n\n")
  • if(!(cmp)(a,b))printf("Equal\n")

24
Arrays
  • Passing arrays to functions
  • Entire array may not be passed as arguments to a
    function. Instead, the address of the array is
    passed to the function.
  • int bar10
  • foo(bar)

25
Arrays
  • Passing arrays to functions
  • Three ways for a function to receive an array.
  • void foo(int x) /pointermost common/
  • void foo(int x10) /sized array/
  • void foo(int x) /unsized array/
  • All three produce identical results an integer
    pointer is received.
  • NOTE the size is not required because C performs
    no bounds checking on arrays.
Write a Comment
User Comments (0)
About PowerShow.com