{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C language - Lecture 6 Page 1 BIL 104E INTRODUCTION TO...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 6, Page 1 Compiled by Ergin TARI BIL 104E INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture 6, Page 2 Compiled by Ergin TARI INFORMATION TO USERS THE NOTES IN THE FOLLOWING SLIDES ARE COMPILED FROM FREE VERSIONS OF THE BOOKS Sams Teach Yourself C in 21 Days (Sams Teach Yourself) By Peter Aitken and Sams Teach Yourself C in 24 Hours ( Published by Sams ) By Tony Zhang. The electronic versions of these and other books can be found in the web pages including http://server11.hypermart.net/davidbook901/data/c/c1f8c0d9.htm http://www.informit.com/itlibrary http://www.free-book.co.uk/computers-internet/programming/c/ http://aelinik.free.fr/c/
Background image of page 2
Lecture 6, Page 3 Compiled by Ergin TARI BIL104E: Introduction to Scientific and Engineering Computing, Summer 2007 Lecture 6 Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture 6, Page 4 Compiled by Ergin TARI Introduction Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings But what is a pointer? A pointer is a variable whose value is used to point to another variable. From this definition, you know two things: first, that a pointer is a variable, so you can assign different values to a pointer variable, and second, that the value contained by a pointer must be an address that indicates the location of another variable in the memory. That's why a pointer is also called an address variable.
Background image of page 4
Lecture 6, Page 5 Compiled by Ergin TARI A program variable is stored at a specific memory address. 1:#include <stdio.h> 2:int main() 3:{ 4: float fl=3.14; 5: printf("%.2f\n", fl); 6: return 0; 7:}
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture 6, Page 6 Compiled by Ergin TARI A program variable is stored at a specific memory address. A variable named rate has been declared and initialized to 100. The compiler has set aside storage at address 1004 for the variable and has associated the name rate with the address 1004.
Background image of page 6
Lecture 6, Page 7 Compiled by Ergin TARI Creating a Pointer You should note that the address of the variable rate (or any other variable) is a number and can be treated like any other number in C. If you know a variable's address, you can create a second variable in which to store the address of the first. The first step is to declare a variable to hold the address of rate. Give it the name p_rate, for example. At first, p_rate is uninitialized. Storage has been allocated for p_rate, but its value is undetermined. Memory storage space has been allocated for the variable p_rate
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture 6, Page 8 Compiled by Ergin TARI Creating a Pointer The next step is to store the address of the variable rate in the variable p_rate. Because p_rate now contains the address of rate, it indicates the location where rate is stored in memory. In C parlance, p_rate points to rate, or is a pointer to rate.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}