Chapter Pointers In the preceding chapters our programs have b een written to access ob jects directly ie using the variable names Wehave p ostp oned until now a discussion of the concept of indirect access ie access of ob jects using their address As wehave seen variables lo cal to a function may b e accessed using their name only within that function When arguments are passed to another function only the values are passed and the cal led function may use these values but cannot aect the variable cells in the cal ling function Sometimes however a function needs to have direct access to the cells in another function This can b e done in C through indirect access using the address of the cell called a p ointer In this chapter we will intro duce the concepts of indirect access p ointer typ es and dereferenced p ointer variables We will use these concepts to write functions that indirectly access ob jects in a calling function What is a Pointer Frequentlya cal led function needs to makechanges to ob jects declared in the cal ling function For example the function scanf needs to access ob jects in the calling function to store the data read and converted into an ob ject dened there Therefore wesupply scanf with the address y function can indirectly access an of ob jects rather than their values Here we will see howan ob ject by its address Another common use of p ointers is to write functions that return more than one value As wehave seen every function in C returns a value as the value of the function however if a functions meaning includes the return of several pieces of information this single return value is not sucient In these cases wecanhave the function return multiple data values indirectly using p ointers CHAPTER POINTERS Data vs Address Before we discuss passing p ointers and indirectly accessing data b etween functions let us lo ok at howwe can declare p ointer variables and access data using them Consider the following simple program main int x int iptr printfTesting Pointer Variablesn x iptr x printfdniptr Wehave declared twointegers xintended to hold an integer value and iptr whichisintended to hold a p ointer to an integer ie and address of an integer We then assign a value to xand the address of x to the variable iptr using the address of op erator The address of a variable is simply the byte address of the cell whichwas allo cated by the declaration An address is an integer actually and unsigned integer so may b e stored in an int typ e variable The situation is shown in Figure a When we compile and execute this program the result is Testing Pointer Variables What if wehadwanted to printthevalue of the cell p ointedtoby iptr and not the value of accesses the ob ject p ointed to by its op erand In our iptr itself The indirection op erator example the value of iptr is which is an address of some ob ject ie iptr p oints to some ob ject lo cated at address So we should b e able to access that ob ject with an expression like iptr However there is no waytoknowhowmanybytes to access at address nor howtointerpret the data unless the typ e of ob ject at address is known is it an intafloatachar etc In order for the compiler to knowhow to access an ob ject indirectlyitmust know the typ e of that ob ject We sp ecify the typ e of ob ject to access by indicating to the compiler the typ e of ob jects a p ointer refers to when we declare the p ointer So in our example we should declare the variable iptr asapointertoaninteger as follows int iptr WHAT IS A POINTER or int iptr white space may separate the op erator andthevariable name iptr The declaration sp ecies avariable iptroftyp e int ieinteger p ointer the typ e is read directly from the declaration So int is the typ e of iptrand int is the typ e of iptr the thing it p oints to This statement declares an integer p ointer variable iptr and allo cates memory for a p ointer variable Similarly we can declare float p ointers or character p ointers float pa pb char pc These statements declare variables pa and pbwhichcanpointto float typ e ob jects and pc which can p ointtoachar typ e ob ject All p ointer variables store addresses which are unsigned integers and so need the same amount of memory space regardless of the p ointer typ es Since the compiler now knows that iptr points to an integer ob ject it can access the ob ject correctly Our simple program b ecomes main int x int iptr printfTesting Pointer Variablesn x iptr x printfAddress d holds value dniptriptr which pro duces the output Testing Pointer Variables Address holds value We are generally not interested in the value of the p ointer variable itself it mayeven b e dierent each time a program is run Instead weareinterested in the cell the p ointerispointing to so we indicate the value of a p ointer variable in diagrams and program traces using an arrowas shown in Figure b In summary the address of an ob ject is called a p ointer to that ob ject since the address tells one where to go in order to access the ob ject The address by itself do es not provide sucient CHAPTER POINTERS main intx int iptr a main intx int iptr b Figure Declaring Pointer Variables WHAT IS A POINTER information to access an ob ject wemust know what typ e of ob ject the address is p ointing to If the p ointer address value and the data typ e of the ob ject that it p oints to are b oth known then it is p ossible to access the ob ject correctly In other words p ointers must b e sp ecied to b e int p ointers p ointing to an integer typ e ob ject float pointers p ointing to a oating p ointtyp e ob ject char p ointers etc Indirect Access of Values The indirection op erator accesses an ob ject of a sp ecied typ e at an address Accessing an ob ject by its address is called indirect accessThus iptr indirectly accesses the ob ject that iptr points to ie iptr accesses x The indirection op erator is also called the contents of op erator or the dereference op erator Applying the indirection op erator to a p ointer variable is referred to as dereferencing the p ointer variable ie iptr dereferences iptr The address of op erator is used to get the address of an ob ject Wehave already used it in calls to scanf We can also use it to assign a value to a p ointer variable Let us consider some examples using the following declarations int x z float y char ch pch int pi pi float pf When these declarations are encountered memory cells are allo cated for these variables at some addresses as shown in Figure Variables x and z are int typ es y is floatand ch is char Pointer variables pi and pi are variables that can p ointtointegers pf is a float pointer and pch isacharacter p ointer Note that the initial values of all variables including p ointer variables are unknown Just as wemust initialize int and float variables wemust also initialize p ointer variables Here are some examples x y z pi x pi points to x pi z pipointstoz pch ch pchpointstoch The result of executing these statements is shown in Figure pi points to the cell for the variable x pi p oints to z pch p oints to chand pf still contains garbage Rememb er the value of a p ointer variable is stored as an address in the cell however we do not need to b e concerned with the v alue itself Instead our gure simply shows what the initialized p ointer variables p oint CHAPTER POINTERS main intx intz oat y char ch int pi intpi oat pf char p ch Figure Declaration of Pointer Variables main intx intz oat y char ch int pi intpi oat pf char p ch Figure Assignments of p ointers WHAT IS A POINTER main intx intz oat y char ch CO C C C C C C C C C C C C int pi intpi oat pf char p ch Figure Eect of Pointer to Pointer Assignment Statement to These initialized p ointers maynow b e used to indirectly access the ob jects they p oint to or they b e maybechanged by new assignments Here are some examples of statements and how they change things for the ab ove memory organization The statements are numb ered in order to reference them the numb ers are not part of the co de pi pi pi points to where pi points ie pi x pi now points to z pi still points to x pi z ie pi z pi x pi pi zxiez pi zxisunchanged pi pi x z is unchanged Statement Assigns value of pi to piso pi now also p oints to x see Figure Since b oth of the variables are typ e int this assignmentisallowed Statement Makes pi pointtoz see Figure The expression z evaluates to the address of z ie an int pointer Statement Since pi points to xthevalue of the right hand side pi dereferences the p ointer and evaluates to the value in the cell ie This value is assigned to the ob ject accessed by the left hand side pi ie the place p ointed to by pi or the CHAPTER POINTERS main intx intz oat y char ch CO C C C C C C C C C C C C int pi intpi oat pf char p ch Figure Eect of Pointer Reassignment Statement ob ject z see Figure This has the same eect as the assignment zx Note wehave used a dereferenced p ointer variable as the Lvalue on the left hand side of an assignment op erator The semantics is to access the ob ject indirectly and store the value of the expression on the righthandside Statement The value is assigned to piie z see Figure Again wehave used an indirect access for the Lvalue of the assignment Statement The right hand side evaluates to since is added to pi so is assigned to pi ie x see Figure Again wehave used an indirect access on b oth
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages38 Page
-
File Size-