Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.

Slides:



Advertisements
Similar presentations
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Advertisements

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Structures in C.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
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.
CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
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.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 7 - Pointers Outline 7.1 Introduction
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
Chapter 7 - Pointers Outline 7.1 Introduction
C Structures, Unions, Bit Manipulations and Enumerations
File Processing (Cont.) and Structures
Chapter 7 - Pointers Outline 7.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Pointers and Pointer-Based Strings
Pointers Kingdom of Saudi Arabia
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
CISC181 Introduction to Computer Science Dr
Structures Declarations CSCI 230
Presentation transcript:

Pointers

Pointer Variable Declarations 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 count 7 7 countPtr

Pointer Variable Declarations and Initialization… Pointer declarations – * used with pointer variables int *myPtr; – Declares a pointer to an int (pointer of type int * ) – Multiple pointers require using a * before each variable declaration int *myPtr1, *myPtr2; – Can declare pointers to any data type – Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing ( NULL preferred)

The Relationship Between Pointers and Arrays Arrays and pointers closely related – Array name like a constant pointer – Pointers can do array subscripting operations Declare an array b[ 5 ] and a pointer bPtr – To set them equal to one another use: bPtr = b; The array name ( b ) is actually the address of first element of the array b[ 5 ] bPtr = &b[ 0 ] Explicitly assigns bPtr to address of first element of b

The Relationship Between Pointers and Arrays – Element b[ 3 ] Can be accessed by *( bPtr + 3 ) – Where n is the offset. Called pointer/offset notation Can be accessed by bptr[ 3 ] – Called pointer/subscript notation – bPtr[ 3 ] same as b[ 3 ] Can be accessed by performing pointer arithmetic on the array itself *( b + 3 )

/* Fig. 7.20: fig07_20.cpp Using subscripting and pointer notations with arrays */ #include int main() { int b[] = { 10, 20, 30, 40 }; /* initialize array b */ int *bPtr = b; /* set bPtr to point to array b */ int i; /* counter */ int offset; /* counter */ /* output array b using array subscript notation */ printf( "Array b printed with:\nArray subscript notation\n" ); /* loop through array b */ for ( i = 0; i < 4; i++ ) { printf( "b[ %d ] = %d\n", i, b[ i ] ); } /* end for */

/* output array b using array name and pointer/offset notation */ printf( "\nPointer/offset notation where\n" "the pointer is the array name\n" ); /* loop through array b */ for ( offset = 0; offset < 4; offset++ ) { printf( "*( b + %d ) = %d\n", offset, *( b + offset ) ); } /* end for */ /* output array b using bPtr and array subscript notation */ printf( "\nPointer subscript notation\n" ); /* loop through array b */ for ( i = 0; i < 4; i++ ) { printf( "bPtr[ %d ] = %d\n", i, bPtr[ i ] ); } /* end for */ /* output array b using bPtr and pointer/offset notation */ printf( "\nPointer/offset notation\n" ); /* loop through array b */ for ( offset = 0; offset < 4; offset++ ) { printf( "*( bPtr + %d ) = %d\n", offset, *( bPtr + offset ) ); } /* end for */ return 0; /* indicates successful termination */ } /* end main */

Array b printed with: Array subscript notation b[ 0 ] = 10 b[ 1 ] = 20 b[ 2 ] = 30 b[ 3 ] = 40 Pointer/offset notation where the pointer is the array name *( b + 0 ) = 10 *( b + 1 ) = 20 *( b + 2 ) = 30 *( b + 3 ) = 40 Pointer subscript notation bPtr[ 0 ] = 10 bPtr[ 1 ] = 20 bPtr[ 2 ] = 30 bPtr[ 3 ] = 40 Pointer/offset notation *( bPtr + 0 ) = 10 *( bPtr + 1 ) = 20 *( bPtr + 2 ) = 30 *( bPtr + 3 ) = 40 Press any key to continue

STRUCTURES

Structures – Collections of related variables (aggregates) under one name Can contain variables of different data types – Commonly used to define records to be stored in files – Combined with pointers, can create linked lists, stacks, queues, and trees

Structures… Example struct card { char *face; char *suit; }; – struct introduces the definition for structure card – card is the structure name and is used to declare variables of the structure type – card contains two members of type char * These members are face and suit

Structure… struct information – A struct cannot contain an instance of itself – Can contain a member that is a pointer to the same structure type – A structure definition does not reserve space in memory Instead creates a new data type used to declare structure variables Declarations – Declared like other variables: card oneCard, deck[ 52 ], *cPtr; – Can use a comma separated list: struct card { char *face; char *suit; } oneCard, deck[ 52 ], *cPtr;

Structure… Valid Operations Assigning a structure to a structure of the same type Taking the address (&) of a structure Accessing the members of a structure Using the sizeof operator to determine the size of a structure

Initializing Structures Initializer lists Example: card oneCard = { "Three", "Hearts" }; Assignment statements Example: card threeHearts = oneCard; Could also declare and initialize threeHearts as follows: card threeHearts; threeHearts.face = “Three”; threeHearts.suit = “Hearts”;

Accessing Members of Structures Accessing structure members Dot operator (.) used with structure variables card myCard; printf( "%s", myCard.suit ); Arrow operator (->) used with pointers to structure variables card *myCardPtr = &myCard; printf( "%s", myCardPtr->suit ); myCardPtr->suit is equivalent to ( *myCardPtr ).suit

Structures and Unions (1/6) Structures (records) – Arrays are collections of data of the same type. In C there is an alternate way of grouping data that permit the data to vary in type. This mechanism is called the struct, short for structure. – A structure is a collection of data items, where each item is identified as to its type and name.

Structures and Unions Create structure data type – We can create our own structure data types by using the typedef statement as below: This says that human_being is the name of the type defined by the structure definition, and we may follow this definition with declarations of variables such as: human_being person1, person2;