CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.

Slides:



Advertisements
Similar presentations
An introduction to pointers in c
Advertisements

Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
ICS312 Set 2 Representation of Numbers and Characters.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Homework Starting Chapter 2 K&R. Read ahead. Questions?
David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Pointers Ethan Cerami Fundamentals of Computer New York University.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Computer Science 210 Computer Organization Pointers.
Pointers CSE 2451 Rong Shi.
Data Representation Prepared by Dr P Marais (Modified by D Burford)
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
ICS312 Set 1 Representation of Numbers and Characters.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
Number System. Number Systems Important Number systems – Decimal – Binary – Hexadecimal.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Computer Organization – Memory Access David Monismith Jan. 30, 2015 Based upon notes by Dr. Bill Siever and the Patterson and Hennessy Text.
Chapter 2 Number Systems: Decimal, Binary, and Hex.
1 Workin’ with Pointas An exercise in destroying your computer.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
What does the following code write to the monitor? #include int main( void ) { int a ; int *pa ; a = 77 ; pa = &a ; printf("a=%d *pa=%d\n", a, *pa ); system("pause");
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
The Hexadecimal System is base 16. It is a shorthand method for representing the 8-bit bytes that are stored in the computer system. This system was chosen.
Computer science C programming language lesson 3.
User-Written Functions
Chapter 8 Arrays, Strings and Pointers
Introduction to Programming Using C
CSE 220 – C Programming Pointers.
Functions and Pointers
Data Representation Binary Numbers Binary Addition
CS1010 Discussion Group 11 Week 9 – Pointers.
EPSII 59:006 Spring 2004.
Pointers.
INC 161 , CPE 100 Computer Programming
COMP3221: Microprocessors and Embedded Systems
Pointers.
Functions and Pointers
INC 161 , CPE 100 Computer Programming
Basic notes on pointers in C
Tejalal Choudhary “C Programming from Scratch” Pointers
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Lecture 18 Arrays and Pointer Arithmetic
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers.
Lecture 2 SCOPE – Local and Global variables
Simulating Reference Parameters in C
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Comp Org & Assembly Lang
Variables, Pointers, and Arrays
ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters

CSE 251 Dr. Charles B. Owen Programming in C2 AddressVariableValue 0x4520B181 0x4524pB0x4520 Today’s Lecture Pointers are variables that store memory addresses Before we learn about pointers, we must learn more about addresses AddressVariableValue 0x4520B181 0x4524pB0x4520 Memory Address PointerValue

CSE 251 Dr. Charles B. Owen Programming in C3 Computer Memory Memory is just a long list of numbers one after the other My laptop has over 4 Billion of these numbers Each number is 8 bits (BYTE) We combine them to make integers and floating point values

CSE 251 Dr. Charles B. Owen Programming in C4 Computer Memory int (21) float (18.2)

CSE 251 Dr. Charles B. Owen Programming in C5 Memory Addresses Memory addresses in computers are often 32 bits (or nowadays, 64-bits) long, e.g Another way to represent an address is to use hexadecimal: 0x 7ffffa8c

CSE 251 Dr. Charles B. Owen Programming in C6 Hexadecimal (Base-16) 0000 = = = = = = 10 = a 0011 = = 11 = b 0100 = = 12 = c 0101 = = 13 = d 0110 = = 14 = e 0111 = = 15 = f I have included this chart on your worksheet so you can refer to it.

CSE 251 Dr. Charles B. Owen Programming in C7 Addresses 32-bit address (Binary): f f f f a 8 c 32-bit address (Hex): 0x 7 f f f f a 8 c Notes: – In C “0x” indicates a Hexadecimal number – Convert every four bits to a hex digit

CSE 251 Dr. Charles B. Owen Programming in C8 Arithmetic (in Hex) Sum: 0x x 04 0x = = = = = = 10 = a 0011 = = 11 = b 0100 = = 12 = c 0101 = = 13 = d 0110 = = 14 = e 0111 = = 15 = f

CSE 251 Dr. Charles B. Owen Programming in C9 Arithmetic (in Hex) Sum with carry: 0x 8c + 0x 04 0x ?? 0000 = = = = = = 10 = a 0011 = = 11 = b 0100 = = 12 = c 0101 = = 13 = d 0110 = = 14 = e 0111 = = 15 = f

CSE 251 Dr. Charles B. Owen Programming in C10 Arithmetic (in Hex) Sum with carry: 0x 8c + 0x 04 0x ?? – What is “c + 4”? – In decimal it is “ = 16” which is Hex “10” (0 and carry 1)

CSE 251 Dr. Charles B. Owen Programming in C11 Arithmetic (in Hex) Sum with carry: 0x 8c + 0x 04 0x ?? – What is “c + 4”? – In decimal it is “ = 16” which is Hex “10” (0 and carry 1) 1

CSE 251 Dr. Charles B. Owen Programming in C12 Arithmetic (in Hex) Sum with carry: 0x 8c + 0x 04 0x 90 – What is “c + 4”? – In decimal it is “ = 16” which is Hex “10” (0 and carry 1) 1 1

CSE 251 Dr. Charles B. Owen Programming in C13 Bytes and Words Remember 8 bits = 1 byte 32 bits = 4 bytes = 1 word. 32-bit address machines are addressed by bytes so consecutive words have addresses that differ by four byte word

CSE 251 Dr. Charles B. Owen Programming in C14 32-bit Addresses Here are three consecutive 32-bit addresses (in Hex) of words: 0x dc bb x x

CSE 251 Dr. Charles B. Owen Programming in C15 Pointers Pointers and Ref parameters Pointers are variables that contain addresses Just like other variables, they must be declared before being used Declaration: int *p; /* instead of int p for integers */ int * means p is a pointer variable that stores the address of an integer variable

CSE 251 Dr. Charles B. Owen Programming in C16 Pointer Initialization Declaration: int a = 2;/* a is an integer */ int *pA = &a;/* pA is a pointer containing the address of a */ “&” operator means “address of” Read it as “at”

CSE 251 Dr. Charles B. Owen Programming in C17 The Address Game int a = 2; int *pA = &a; a pA An Intel processor is called a little endian processor because it stores values with the least significant byte first. You read it in reverse order. 0x in memory will be:

CSE 251 Dr. Charles B. Owen Programming in C18 Example Program “%x” prints the hexadecimal value int a = 21; int *pA = &a; printf("%d\n", a); printf("%x\n", a); printf("%x\n", &a); printf("%x\n", pA); printf("%d\n", *pA); printf("%x\n", &pA); bfee861c 21 bfee8618 Output Operators: &“address of” *“dereference”

CSE 251 Dr. Charles B. Owen Programming in C19 #include int main() { int a = 15, b = 38; int *c = &a; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); a = 49; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); c = &b; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); }

CSE 251 Dr. Charles B. Owen Programming in C20 #include int main() { int a = 15, b = 38; int *c = &a; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); a = 49; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); c = &b; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); }

CSE 251 Dr. Charles B. Owen Programming in C21 First Section int a = 15, b = 38; int *c = &a; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); AddressMemory 0xeffffa94 0xeffffa90 0xeffffa8c Name a b c xeffffa94 Declares a and b as integers Declares c as a pointer that contains the address of a (“points to a”)

CSE 251 Dr. Charles B. Owen Programming in C22 First Section int a = 15, b = 38; int *c = &a; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); AddressMemory 0xeffffa94 0xeffffa90 0xeffffa8c Name a b c xeffffa94 Declares a and b as integers Declares c as a pointer that contains the address of a (“points to a”) Output: effffa94 15 effffa90 38 effffa8c effffa94 15

CSE 251 Dr. Charles B. Owen Programming in C23 First Section int a = 15, b = 38; int *c = &a; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); Declares a and b as integers Declares c as a pointer that contains the address of a (“points to a”) Output: effffa94 15 effffa90 38 effffa8c effffa94 15 Note the difference between *C in variable declaration and *C in printf

CSE 251 Dr. Charles B. Owen Programming in C24 #include int main() { int a = 15, b = 38; int *c = &a; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); a = 49; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); c = &b; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); }

CSE 251 Dr. Charles B. Owen Programming in C25 Second Section AddressMemory 0xeffffa94 0xeffffa90 0xeffffa8c Name A B C xeffffa94 a = 49; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c);

CSE 251 Dr. Charles B. Owen Programming in C26 Second Example AddressMemory 0xeffffa94 0xeffffa90 0xeffffa8c Name A B C xeffffa94 a = 49; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); Output: effffa94 49 effffa90 38 effffa8c effffa94 49

CSE 251 Dr. Charles B. Owen Programming in C27 #include int main() { int a = 15, b = 38; int *c = &a; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); a = 49; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); c = &b; printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); }

CSE 251 Dr. Charles B. Owen Programming in C28 Third Section AddressMemory 0xeffffa94 0xeffffa90 0xeffffa8c Name A B C xeffffa90 c = &b; /* c now points to b */ printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c);

CSE 251 Dr. Charles B. Owen Programming in C29 Third Section AddressMemory 0xeffffa94 0xeffffa90 0xeffffa8c Name A B C xeffffa90 c = &b; /* c now points to b */ printf("%x : %d\n", &a, a); printf("%x : %d\n", &b,b); printf("%x : %x : %d\n", &c, c, *c); Output: effffa94 49 effffa90 38 effffa8c effffa

CSE 251 Dr. Charles B. Owen Programming in C30 Reference parameters A valuable use for pointers: Passing addresses to a function

CSE 251 Dr. Charles B. Owen Programming in C31 Argument & Returned Value Consider a function call y=f(x). – The value x is passed to the function f – A value is returned and assigned to y. – By passed we mean that the value of argument x is copied to the parameter in the function. Some calculation is performed and the result is returned and assigned to y.

CSE 251 Dr. Charles B. Owen Programming in C32 Example int x, y; x = 5; y = Square(x); int Square(int t) { return t*t } AddressMemory 0xeffffa94 Name … … … … … x y 0xeffffa98

CSE 251 Dr. Charles B. Owen Programming in C33 Example int x, y; x = 5; y = Square(x); int Square(int t) { return t*t } AddressMemory 0xeffffa94 Name 5 … … … … x y 0xeffffa98

CSE 251 Dr. Charles B. Owen Programming in C34 Example int x, y; x = 5; y = Square(x); int Square(int t) { return t*t } AddressMemory 0xeffffa94 Name 5 … … … 5 x y 0xeffffa98 0xeffffc8c t The call Square(x) : creates a variable t copies the value of x to t

CSE 251 Dr. Charles B. Owen Programming in C35 Example int x, y; x = 5; y = Square(x); int Square(int t) { return t*t } AddressMemory 0xeffffa94 Name 5 … … … 5 x y 0xeffffa98 0xeffffc8c t The call Square(x) : creates a variable t copies the value of x to t calculates t * t returns t 0xeffffc90 25temp

CSE 251 Dr. Charles B. Owen Programming in C36 y=f(x) Only one valued returned What if we want to return more than one value? – Solution is to use pointers to variables in the calling function

CSE 251 Dr. Charles B. Owen Programming in C37 How to do this in C The approach is to pass the address (using the & operator) of the value to be modified. We call such a parameter a reference parameter. Use the * operator to change the reference parameter value

CSE 251 Dr. Charles B. Owen Programming in C38 Function Reference Params int val = 10; MyFun(&val); printf(“%d”,val); void MyFun(int *param) { *param = 27; } NameAddressValue val 0xeffffa90 10

CSE 251 Dr. Charles B. Owen Programming in C39 Function Reference Params int val = 10; MyFun(&val); printf(“%d”,val); void MyFun(int *param) { *param = 27; } NameAddressValue val 0xeffffa90 10

CSE 251 Dr. Charles B. Owen Programming in C40 Function Reference Params int val = 10; MyFun(&val); printf(“%d”,val); void MyFun(int *param) { *param = 27; } NameAddressValue val 0xeffffa90 27

CSE 251 Dr. Charles B. Owen Programming in C41 Function Reference Params int val = 10; MyFun(&val); printf(“%d”,val); void MyFun(int *param) { *param = 27; } NameAddressValue val 0xeffffa90 27 Prints: 27 The memory used by the function is destroyed when it returns.

CSE 251 Dr. Charles B. Owen Programming in C42 What will this do different? int val = 10; MyFun2(val); printf(“%d”,val); void MyFun2(int param) { param = 27; } NameAddressValue val 0xeffffa90 10

CSE 251 Dr. Charles B. Owen Programming in C43 Cards program /* Create a random card and suit */ /* This will compute a random number from 0 to 3 */ suit1 = rand() % 4; /* This will compute a random number from 1 to 13 */ card1 = rand() % ; do { /* Create a random card and suit */ /* This will compute a random number from 0 to 3 */ suit2 = rand() % 4; /* This will compute a random number from 1 to 13 */ card2 = rand() % ; } while(card1 == card2 && suit1 == suit2); This program repeats code. We don’t like to do that. But, we could not put the card draw into a function because a function can only return one value, or so we thought!

CSE 251 Dr. Charles B. Owen Programming in C44 Solution, pass by reference using pointers /* Create a random card and suit */ DrawCard(&card1, &suit1); do { DrawCard(&card2, &suit2); } while(card1 == card2 && suit1 == suit2); void DrawCard(int *card, int *suit) { /* This will compute a random number from 0 to 3 */ *suit = rand() % 4; /* This will compute a random number from 1 to 13 */ *card = rand() % ; } Don’t forget: *suit  to set the value &card1  to get the address Pass with & Set with * 3