# Pointer (I) - PowerPoint PPT Presentation

1 / 13

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Pointer (I)

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

## 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

>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

### Agenda

• What is a pointer?

• Declare the type of pointers.

• Use of Pointers.

### What is a pointer?

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

• Code and Data segments

John Tim Jane Robert Ben Mary Lisa

101 102 103 104 105 106 107

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?

• Pass as argument or Return the value

• Dynamic variables

• More efficient

• Refer to part of the a larger variable

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

• 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

• 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 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

• What is a pointer?

• Declare the type of pointers.