Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.

Slides:



Advertisements
Similar presentations
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Advertisements

1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
1 Pointers Revisited Linda M c Iver. 2 Pointer Examples int i = 0; 0x2000 0x2001 0x2002 0x2003.
1 ICS103 Programming in C Lecture 10: Functions II.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Computer Science 210 Computer Organization Pointers.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Pointers CSE 2451 Rong Shi.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Pointers *, &, array similarities, functions, sizeof.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Week 12 Methods for passing actual parameters to formal parameters.
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.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Lecture 5 Pointers 1. Variable, memory location, address, value
Computer Science 210 Computer Organization
UNIT 5 C Pointers.
Functions and Pointers
Lesson One – Creating a thread
POINTERS.
ICS103 Programming in C Lecture 10: Functions II
Pointers and Pointer-Based Strings
Pointers.
Pointer.
Lecture 6 C++ Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Pointers.
Functions and Pointers
Computer Science 210 Computer Organization
14th September IIT Kanpur
Object Oriented Programming COP3330 / CGS5409
5.1 Introduction Pointers Powerful, but difficult to master
Simulating Reference Parameters in C
CSCE 206 Lab Structured Programming in C
Pointers and Pointer-Based Strings
POINTER CONCEPT 4/15/2019.
ICS103 Programming in C Lecture 10: Functions II
CSCE 206 Lab Structured Programming in C
ICS103 Programming in C Lecture 10: Functions II
POINTER CONCEPT 8/3/2019.
Presentation transcript:

Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference

Basic Concept of Pointers A pointer is a variable which directly points to a memory address. It will allow the programmer to directly manipulate the data in memory. So far, we have seen that a variable is used to store a value. A pointer variable, however, does not store a value but instead store the address of the memory space which contain the value. Why would we want to use pointers? –To call a function by reference so that the data passed to the function can be changed inside the function. –To create a dynamic data structure which can grow larger or smaller as necessary.

Memory contentMemory address 0x0000 0x0001 0x0002 0x0003 …... 0xFFFF 0xFFFE 0xFFFD 0xFFFC x A variable of type int is stored here. The address of this location is 0x0002. This location contains the value 0x0002. Therefore this location stores a pointer variable which points to the address 0x0002.

Pointer Declaration General format: data_type *ptr_name; Example: –Pointer to an int: int *intptr; –Pointer to a char: char *charptr; The asterik (*) character in front of the variable name tells that the variable is a pointer and not a variable to store value. To prevent the pointer from pointing to a random memory address, it is advisable that the pointer is initialized to 0 or NULL before being used.

Pointer Operator (& and *) When a pointer is created, it is not pointing to any valid memory address. Therefore, we need to assign it to a variable’s address by using the & operator. This operator is called a reference operator. After a pointer is assigned to a particular address, the value in the pointed address can be accessed using the * operator. This operator is called a dereference operator. Look at this example: int num = 9; int *num_ptr; num_ptr = # printf(“num = %d”, *num_ptr); Output: num = 9

#include void main(void) { int var = 10; int *ptrvar; ptrvar = &var; printf(“The address of the variable var is: %d\n”, &var); printf(“The value in the pointer ptrvar is: %d\n”, ptrvar); printf(“Both values are the same – address of var\n”); printf(“The value in the variable var is: %d\n”, var); printf(“The value in the address pointed by ptrvar is: %d\n”, *ptrvar); printf(“Both values are the same – the value in var \n”); printf(“The value in the address of var is: %d\n”, *&var); printf(“The value in the address pointed by ptrvar is: %d\n”, *ptrvar); printf(“Both values are the same – the value in the address of var\n”); }

Parameter Passing by Reference A function call by reference can be done by passing a pointer to the function argument (the same way as passing a normal variable to the argument). When the value referenced by the pointer is changed inside the function, the value in the actual variable will also change. Therefore, we can pass the result of the function through the function argument without having to use the return statement. In fact, by passing pointers to the function argument, we can return more than one value.

When a pointer is passed to a function, we are actually passing the address of a variable to the function. Since we have the address, we can directly manipulate the data in the address. In the case where a non-pointer variable is passed, the function will create another space in memory to hold the value locally while the program is inside the function. Therefore, any change to the variable inside the function will not change the actual value of the variable.

#include void Func1(int, int); void Func2(int *, int *); void main(void) { int a = 8, b = 9; printf(“Before Func1 is called, a = %d, b = %d\n”, a, b); Func1(a, b); printf(“After Func1 is called, a = %d, b = %d\n”, a, b); printf(“\nBefore Func2 is called, a = %d, b = %d\n”, a, b); Func2(&a, &b); printf(“After Func2 is called, a = %d, b = %\nd”, a, b); } void Func1(int a, int b) { a = 0; b = 0; printf(“Inside Func1, a = %d, b = %d\n”, a, b); }

void Func2(int *pa, int *pb) { *pa = 0; *pb = 0; printf(“Inside Func2, *pa = %d, *pb = %d\n”, *pa, *pb); } Output: Before Func1 is called, a = 8, b = 9 Inside Func1, a = 0, b = 0 After Func1 is called, a = 8, b = 9 Before Func2 is called, a = 8, b = 9 Inside Func2, *pa = 0, *pb = 0 After Func2 is called, a = 0, b = 0