POINTERS.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 (Pointers) Lecture 1 –Follow class notes –Some.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
1 CSE1301 Computer Programming Lecture 16 Pointers.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Lecture 6 – Functions (2). Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
UniMAP SemII-09/10EKT120: Computer Programming1 Week 6 – Functions (2)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Lecture 17: The Last Puzzle Piece with Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
PGT 106 C Programming POINTERS. PGT 106 C Programming Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Lecture 9 - Pointers 1. Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference Pointer.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointers Introduction CSCI 230
Computer Science 210 Computer Organization
Week 9 - Pointers.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
Functions and Pointers
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
EKT120: Computer Programming
Week 6 – Functions (2) PGT C Programming.
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
INC 161 , CPE 100 Computer Programming
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
EKT120: Computer Programming
CSI-121 Structured Programming Language Lecture 16 Pointers
Functions and Pointers
Computer Science 210 Computer Organization
Pointer Data Type and Pointer Variables
Pointers and Pointer-Based Strings
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
POINTERS.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
CISC181 Introduction to Computer Science Dr
Pointers and pointer applications
C Pointers Another ref:
Presentation transcript:

POINTERS

Outline Introduction Pointer Variable Definitions and Initialization Pointer Operators Calling Functions by Reference

Introduction Pointer is the address(i.e. a specific memory location) of an object It can refer to different objects at different times Pointers are used in C programs for a variety of purposes: To return more than one value from a function(using pass by reference) To create and process strings To manipulate the contents of arrays and structures

Pointer Variable Definitions and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value num 7 num 7 numPtr

Pointer Variable Definitions and Initialization Pointer definitions * used with pointer variables int *numPtr; Defines a pointer to an int (pointer of type int *) Multiple pointers require using a * before each variable definition int *numPtr1, *numPtr2; Can define pointers to any data type Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing (NULL preferred) int *numPtr = NULL; OR int *numPtr = 0;

Pointer Operators Symbol & is called address operator Returns address of operand int num = 7; int *numPtr; numPtr = # /* numPtr gets address of num */ numPtr “points to” num numPtr num 7 500000 600000 Address of num is value of numPtr

Pointer Operators Symbol * is called indirection/dereferencing operator Returns a synonym/alias of what its operand points to *numPtr returns num (because numPtr points to num) * can also be used for assignment Returns alias to an object *numPtr = 10; /* changes num to 10 */ show pictures!! Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are inverses They cancel each other out

Sample program *numPtr = 15; number = 7 numPtr points to num whereby the value is = 7 Address of numPtr : 1245060 Contents of numPtr : 1245064 Address of num : 1245064 Dereferencing pointer, *numPtr = 15 num = 20 *numPtr = 20 *numPtr + num1 = 25 #include <stdio.h> int main() { int num; int *numPtr; int num1=5; num = 7; printf("number = %d\n", num); numPtr = &num; printf("numPtr points to num whereby the value is = %d\n",*numPtr); printf("Address of numPtr : %d Contents of numPtr : %d\n", &numPtr, numPtr); printf("Address of num : %d\n\n", &num); *numPtr = 15; printf("Dereferencing pointer, *numPtr = %d\n", *numPtr); num = num + num1; printf(“num = %d\n”, num); printf("*numPtr = %d\n", *numPtr); printf("*numPtr + num1 = %d\n", *numPtr + num1); return 0; }

Calling Functions by Reference Call by reference with pointer arguments Pass address of argument using & operator Allows you to change actual location in memory Arrays are not passed with & because the array name is already a pointer * operator Used as alias/nickname for variable inside of function void fun1( int *number ) { *number = 2 * ( *number ); } *number used as nickname for the variable passed

Enter character : f Do you want to continue?y Enter character : I Enter character : k Do you want to continue?n Number of vowel : 1 Number of consonant : 2 Remember..last time #include <stdio.h> #include <string.h> char read(); void find_count_vc(char, int*, int*); void print(int,int); int main() { char ch, choice; int count_v=0,count_c=0; do { ch = read(); find_count_vc(ch, &count_v, &count_c); printf("Do you want to continue?"); scanf("%c", &choice); getchar(); }while((choice == 'y') ||(choice =='Y')); print(count_v,count_c); return 0; } char read() { char ch1; printf("Enter character : "); scanf("%c", &ch1); return(ch1); void find_count_vc(char ch1, int *vowel, int *consonant) { switch(ch1) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': *vowel = *vowel +1;break; default: *consonant = *consonant + 1; } void print(int vowel, int consonant) printf("Number of vowel : %d\n", vowel); printf("Number of consonant : %d\n", consonant); Functions that “return” more than one value i.e. arguments are passed by ref