Docsity

C Programming: Understanding Pointers - Lecture 28, Quizzes of Computer Science

Partial preview of the text

Download C Programming: Understanding Pointers - Lecture 28 and more Quizzes Computer Science in PDF only on Docsity!

Pointer (I)CGS 3460, Lecture 28Mar 22, 2006Hen-I Yang

Previously… „^ Debug

Agenda „^ What is a pointer? „^ Declare the type of pointers. „^ Address and Indirection Operators. „^ Use of Pointers.

What is a pointer? „^ Most important, most abstract, mostmisunderstood concept in C language. „^ Memory address and bytes. „^ Code and Data segmentsJohn^ Tim^ Jane

Robert^ Ben^ Mary

Lisa 101 102 103

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 „^ We can declare the type of pointer as anyof the types we have introduced so far. „^ Which means int, float or charint a;int b[3];char c;int * p1;char * p2;p1 = &b[0];p2 = &c;p1++;p1++;

p1ab p1 p2c b

Use of Pointers: as argument „^ Pointer as argument^

^ So the function can modify the value of theargument 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;elsereturn b;} int *p, x, y;p = max (&x, &y); „^ Do not return local variable or local pointervariable

  • Before you go „ Read Chapter 11. „ Exercise: 11.1, 11.2, 11.4 „ Start working on Homework 4. It will takea much longer time than previous ones. „ Use the sample code (exec_seq3.c) givenin last lecture to figure out problem 2 inquiz