+ Pointers. + Content Address of operator (&) Pointers Pointers and array.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
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.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Introduction to C Programming CE
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
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.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
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”
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
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.
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.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Intro to Pointers in C CSSE 332 Operating Systems
CSE 220 – C Programming Pointers.
Pointers and Pointer-Based Strings
INC 161 , CPE 100 Computer Programming
Pointers and References
Object Oriented Programming COP3330 / CGS5409
Systems Programming Concepts
Lecture 18 Arrays and Pointer Arithmetic
Overloading functions
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers and pointer applications
Variables and Constants
Presentation transcript:

+ Pointers

+ Content Address of operator (&) Pointers Pointers and array

+ The & Operator: Familiar example A familiar program Could it be this way? ? Why not?

+ The & Operator: Familiar example A familiar program Could it be this way? ? Why not? Return type have to be known before the function call; Return value is already in use to notify whether the call was successful or not. So we have to sacrifices it, if we want to return a value; Number of return values cannot vary. Because:

+ Finding Addresses: The & Operator scanf() uses addresses & for arguments. The unary & operator gives you the address where a variable is stored. %p is the specifier for addresses 24 0B76will be printed out on the screen The address where pooh is stored is 0B76 Any C function that modifies a value in the calling function without using a return value uses addresses! You can think of the address as a location in memory.

+ How to see where variables are stored? the two poohs and bahs have different addresses just the value was transferred

+ Altering Variables in the Calling Function Suppose you have two variables called x and y and you want to swap their values: temp = x; x = y; y = temp; Nothing changed! Why?

+ Declaring Pointers A pointer is a variable (or, more generally, a data object) whose value is a memory address. int * pi; /* pi is a pointer to an integer variable */ char * pc; /* pc is a pointer to a character variable */ float * pf, * pg; /* pf, pg are pointers to float variables */ The declaration float* sunmass; says that sunmass is a pointer and that *sunmass is type float i.e. the value occupies 4 bytes starting from address sunmass; Memory can be represented as series of bytes Machine address that pointer contains 4 bytes

+ Pointers ptr = &pooh; /* assigns pooh's address to ptr */ ptr = &bah; /* makes ptr point to bah instead of to pooh */ Now you can use the indirection operator (dereferencing operator) * to find the value stored in bah. val = *ptr; /* finding the value ptr points to */ The statements ptr = &bah; val = *ptr; taken together amount to the following statement: val = bah;

+ Using Pointers to Communicate Between Functions The program that uses pointers to make the interchange() function work. pointers Get memory address of variables

+ Pointers and Arrays short double pointers + 0: 0x0064fd20 0x0064fd28 pointers + 1: 0x0064fd22 0x0064fd30 pointers + 2: 0x0064fd24 0x0064fd38 pointers + 3: 0x0064fd26 0x0064fd40

+ Pointers and Arrays The value of a pointer is the address of the object to which it points. How the address is represented internally is hardware dependent. The address of a large object, such as type double variable, typically is the address of the first byte of the object. Applying the * operator to a pointer yields the value stored in the pointed-to object. Adding 1 to the pointer increases its value by the size, in bytes, of the pointed-to type. dates +2 == &date[2] /* same address */ *(dates + 2) == dates[2] /* same value */

+ Troubles with pointers List of some pointer errors Assigning values to uninitialized pointers int *p, m = 100; *p = m;// Error Assigning values to a pointer variable int *p, m = 100; p = m;// Error Not dereferencing a pointer when required int *p, x = 100; *p = &x; printf(“%d”, p);// Error Assigning the address of an uninitialized variable int m, *p; p = &m; Comparing pointers that point to different objects char name1 [20], name2 [30]; char *p1 = name1; char *p2 = name; If (p1 > p2)… //Error

+ Pointers as functions arguments 1. The function parameters are declared as pointers (for example to pass arrays); 2. The dereferenced pointers are used in the function body; 3. When the function is called, the addresses are passed as actual arguments. array passed as a pointer Example 1: Sorting function Example 2: copying strings

+ Functions returning pointers The function larger receives the addresses of the variables a and b, decides which one is larger using the pointers x and y and the returns the address of its location. The returned value is then assigned to the pointer.

+ (*)Pointers to functions A pointer to a function is declared as follows: type (*fptr) (); This tells the compiler that fptr is a pointer to a function, which returns type value. Assigning the name of the function to the pointer double mul(int, int); double (*p1)(); p1 = mul; Now we can call the function by the pointer p1 with the list of parameters. (*p1)(x,y)  mul(x,y)

+ Example: A program that uses a function pointer as a function argument A program prints the function values over a given range of values. The printing is done by the function table by evaluating the function passed to it by the main.