Pointer i
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Pointer (I) PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on
  • Presentation posted in: General

Pointer (I). CGS 3460, Lecture 28 Mar 22, 2006 Hen-I Yang. Previously…. Debug. Average: 63.3 Max: 92, Min: 16 81.3% get 90% or more. Grading Scale: Hw_Quiz 3 >70: 100% of the grade for homework 50 -- 69: 90% of the grade for homework  40 -- 49: 80% of the grade for homework 

Download Presentation

Pointer (I)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Pointer i

Pointer (I)

CGS 3460, Lecture 28

Mar 22, 2006

Hen-I Yang


Previously

Previously…

  • Debug


Result of quiz 3

Average: 63.3

Max: 92, Min: 16

81.3% get 90% or more

Grading Scale: Hw_Quiz 3

>70: 100% of the grade for homework

50 -- 69: 90% of the grade for homework 

40 -- 49: 80% of the grade for homework 

30 -- 39: 60% of the grade for the homework 

39 or below: at most 60% of the grade for the homework

Result of Quiz 3


Agenda

Agenda

  • What is a pointer?

  • Declare the type of pointers.

  • Address and Indirection Operators.

  • Use of Pointers.


What is a pointer

What is a pointer?

  • Most important, most abstract, most misunderstood concept in C language.

  • Memory address and bytes.

  • Code and Data segments

John Tim Jane Robert Ben Mary Lisa

101 102 103 104 105 106 107


Pointer i

p1

p2

101 102 103 105 106 107

int a;

int b[3];

char c;

int * p1;

char * p2;

p1 = &a;

p2 = &c;

a

b

c

b


Why do we use pointers

Why do we use pointers?

  • Pass as argument or Return the value

  • Dynamic variables

  • More efficient

  • Refer to part of the a larger variable


Declare the type of a pointer

p1

p1

p2

Declare the Type of a Pointer

  • We can declare the type of pointer as any of the types we have introduced so far.

  • Which means int, float or char

int a;

int b[3];

char c;

int * p1;

char * p2;

p1 = &b[0];

p2 = &c;

p1++;

p1++;

a

b

c

b


Address and indirection operator

Address and Indirection Operator

  • Address operator: &

  • Indirection operator: *

  • &: Where does Robert lives? (&Robert = 104)

  • *: Who lives in room 101?

John Tim Jane Robert Ben Mary Lisa

101 102 103 104 105 106 107


Use of pointers as argument

Use of Pointers: as argument

  • Pointer as argument

    • So the function can modify the value of the argument passed

  • C functions are “Pass by Value”

  • scanf(“%d”, &i);

  • Const modifier


Use of pointers as return value

Use of Pointers: as return value

  • Use pointer as return value

    int *max (int *a, int *b) {

    if (*a > *b)

    return a;

    else

    return b;

    }

    int *p, x, y;

    p = max (&x, &y);

  • Do not return local variable or local pointer variable


Summary

Summary

  • What is a pointer?

  • Declare the type of pointers.

  • Address and Indirection Operators.

  • Use of Pointers.


Before you go

Before you go

  • Read Chapter 11.

  • Exercise: 11.1, 11.2, 11.4

  • Start working on Homework 4. It will take a much longer time than previous ones.

  • Use the sample code (exec_seq3.c) given in last lecture to figure out problem 2 in quiz 3


  • Login