ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.

Slides:



Advertisements
Similar presentations
Structures Spring 2013Programming and Data Structure1.
Advertisements

Chapter 7: User-Defined Functions II
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Learners Support Publications Classes and Objects.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers.
Pointers and References
Classes and Objects.
Data Structures and Algorithms Introduction to Pointers
Submitted By : Veenu Saini Lecturer (IT)
Chapter 9: Pointers and String
Pointers and References
Structures, Unions, and Enumerations
Introduction to Pointers
Presentation transcript:

ECE 353: Lab C Pointers and Structs

Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration that x is a pointer to a variable of type int – x contains the location that the pointer is pointing to – Referencing operator: & & means “address of.” – If we declare int y, then &y is the address of y

Given a definition of the form int *i – &i is the address of the pointer itself – i is the address this pointer points to – *i is the content of the address the pointer points to Declaring a pointer does NOT automatically initialize it: be careful to avoid dangling pointers

Pointer Arithmetic Valid operations: – Assignment of pointers of the same type. – Addition or subtraction by an integer: all results are in units of the type to which the pointer points – Subtracting or comparing two pointers to members of the same array (the compiler is not obliged to warn you if they are not to the same array) – Zero assignment or comparison with zero Illegal operations: – Adding two pointers – Multiplication, division, shifting, masking – Adding by a non-integer quantity – Assignment of pointers of different types without a cast (except for void *)

Precedence Table The precedence table specifies the order in which operations are carried out – Is the value of 3+4*4 equal to 28 or to 19? – Is the condition (a&b==1) different from ((a&b)==1)? – If we had declared i as a pointer to int, Is *i+1 different from *(i+1)? Is *i++ different from *i+1? Is *i++ different from (*i)++? Is *i++ different from ++*i? Is *i() different from (*i)()? What does 10**i mean? If j is another pointer to int, what is *i-*j? Is the declaration int *k[100] an array of pointers or a pointer to an int array? What does (*(void(*)())0)() mean? (From Koenig: C Traps and Pitfalls, 1989)

Pointers and Arrays The name of the array is a pointer to the start of that array. – If a[10] is declared as an array of ints, a is a pointer to the first element of that array. However, an array name is NOT a variable: you cannot assign a pointer to an array name or increment it with ++. – Because of the way array accesses are implemented, 3[a] means the same thing as a[3] (since *(a+3) is the same as *(3+a)).

Strings There is no string type built into the language – Strings are processed as character arrays – String literals are specified between double-quotes – These arrays are stored with a end-delimiter of \0. – Pointers are often used with strings – string.h should be included for access to the relevant library functions Example: char *roomNumber; roomNumber = “ELAB 303”;

Pointers to Pointers int **p means that p is a pointer to a pointer to data of type int There is a similarity between pointers to pointers and two-dimensional arrays – However, a pointer is NOT an array. – Storage: Global & static variables: Data segment Dynamic memory: Heap Local variables: Stack

const const indicates to the compiler that you will not change the item so qualified. Examples: – const int x; – int *const y; – const int *a; – const int *const b;

Examples of Declarations int **a; // ptr to ptr int *b[10]; // array of ptrs int (*c)[10]; // ptr to array int *d(); // d returns ptr to int int (*e)(); // e is ptr to int function char (*(*x())[])(); // ? char (*(*y[3])())[5]; // ?? Kernighan & Ritchie, The C Programming Language, 1988

structs Keyword struct followed by an optional structure tag (or name) followed by struct members within braces. – A struct member can be the same as the tag: the context disambiguates them. – A struct member may itself be a struct – Reference to a member is via structure_name.member_name

Legal Operations on structs Copying Assignment Taking its address Accessing its members Passing to, and returning it from, a function call – Structure parameters are passed by value

Pointers to structs We can declare pointers to structs in the usual way. – If q is a struct type, then the declaration struct q *ptr; defines a pointer to this type – Question: If x is a field of struct type q, is (*ptr).x different from *ptr.x?

-> notation -> is shorthand: if ptr is a pointer to a structure of which x is a member, then ptr -> x refers to member x of the struct pointed to by ptr The usual precedence conditions apply: – Is ++ptr->x the same as ++(ptr->x)? – Is ++ptr->x the same as (++ptr)->x?