C Pointers Another ref:

Slides:



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

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
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.
 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.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
[S. Uludag] CIS / CSC 175 Problem Solving and Programming I Winter 2010 Suleyman Uludag Department of Computer Science, Engineering and Physics (CSEP)
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
 2007 Pearson Education, Inc. All rights reserved. 1 C Pointers Chapter 7 from “C How to Program" Another ref:
 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.
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.
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. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
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.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers Introduction CSCI 230
Week 9 - Pointers.
Pointers Introduction
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
POINTERS.
POINTERS.
Command Line Arguments
EPSII 59:006 Spring 2004.
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Introduction to Programming
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
typedef typedef int Index; typedef char Letter; Index i; i = 17;
Pointers and Pointer-Based Strings
Lecture 6 C++ Programming
8 Pointers.
Pointers and Pointer-Based Strings
Chapter 7 from “C How to Program"
Topics discussed in this section:
Pointers and Pointer-Based Strings
Programming in C Pointer Basics.
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
Introduction to Problem Solving and Programming
5.1 Introduction Pointers Powerful, but difficult to master
7 C Pointers.
Pointers and Pointer-Based Strings
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
C++ Pointers and Strings
EENG212 ALGORITHMS & DATA STRUCTURES
C++ Programming Lecture 17 Pointers – Part I
POINTERS.
Lecture 2 Arrays & Pointers May 17, 2004
Chapter 9: Pointers and String
Functions Reasons Concepts Passing arguments to a function
CISC181 Introduction to Computer Science Dr
C++ Pointers and Strings
Pointers and pointer applications
Presentation transcript:

C Pointers Another ref: http://pw1.netcom.com/~tjensen/ptr/pointers.htm

Fig. 7.1 | Directly and indirectly referencing a variable.

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

Common Programming Error 7.1 The asterisk (*) notation used to declare pointer variables does not distribute to all variable names in a declaration. Each pointer must be declared with the * prefixed to the name; e.g., if you wish to declare xPtr and yPtr as int pointers, use int *xPtr, *yPtr;.

Good Programming Practice 7.1 Include the letters ptr in pointer variable names to make it clear that these variables are pointers and thus need to be handled appropriately.

Error-Prevention Tip 7.1 Initialize pointers to prevent unexpected results.

Assignment revisited – Pointers and Vars X = 17; lvalue = rvalue lvalue: expression that evaluates to a location rvalue: expression that evaluates to a value

Simple Pointers 23 number 003F45A8 ptr_to_num Pointer is a value that points to a location in the memory Pointer is associated with a type int number ; int * ptr_to_num ; number = 23; ptr_to_num = & number; printf("Value is %d \n", (*ptr_to_num) ); 23 number 003F45A8 ptr_to_num

More Pointers int number ; int * p1, * p2; p1 = & number ; printf(" *p1 = %d *p2 = %d ", *p1, *p2); /* Output ?? */ number p1 p2

Pointers and Arrays char str[32]; char *ptr; ptr = str ; strcpy( str, "test" ); strcpy( ptr, "test" ); /* does the same as above */

Pointers and Arrays int table [8]; int *ptr ; ptr = table; How about ptr = & table[0]?? vs. ptr=table;?? 94 table ptr ( ptr + 4 )

Pointer operations Can add and subtract numbers (like array indices) Can increment and decrement! char str[] = "Test"; char * p ; int i; for( p = str, i=0; *p != '\0'; p++, i++); printf(" The length of the string is %d ", i);

NULL pointer A way to tell that pointer points to nothing void main() { char ar[5]; char *msg = NULL; MyPrint( msg ); //MyPrint( ar ); *msg=“Hello”; } //void MyPrint( char myar[ ]) void MyPrint( char * txt ) if ( txt == NULL ) printf( "Invalid parameters: NULL pointer received\n"); else printf( "%s\n", txt ); }

Command Line Arguments /* MyProg.c */ int main ( int argc , char *argv[] ) { ... > myProg one two three argc = 4 argv[0] = "myProg" argv[1] = "one" argv[2] = "two" argv[3] = "three“ argv[4] = NULL

3 Pointer Operators & (address operator) Returns address of operand int y = 5; int *yPtr; yPtr = &y; /* yPtr gets address of y */ yPtr “points to” y

Fig. 7.2 | Graphical representation of a pointer pointing to an integer variable in memory.

Fig. 7.3 | Representation of y and yPtr in memory.

3 Pointer Operators * (indirection/dereferencing operator) Returns a synonym/alias of what its operand points to *yptr returns y (because yptr points to y) * can be used for assignment Returns alias to an object *yptr = 7; /* changes y to 7 */ Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are inverses They cancel each other out

Common Programming Error 7.2 Dereferencing a pointer that has not been properly initialized or that has not been assigned to point to a specific location in memory is an error. This could cause a fatal execution-time error, or it could accidentally modify important data and allow the program to run to completion with incorrect results.