Lecture 18 Arrays and Pointer Arithmetic

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

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:
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
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.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Values, variables and types © Allan C. Milne v
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
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.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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:
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Pointers *, &, array similarities, functions, sizeof.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Introduction to C Programming CE Lecture 6 Functions, Parameters and Arguments.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
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
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter 9: Pointers.
Hassan Khosravi / Geoffrey Tien
Arrays 2/4 By Pius Nyaanga.
Numeric Arrays Numeric Arrays Chapter 4.
CNG 140 C Programming (Lecture set 10)
Hassan Khosravi / Geoffrey Tien
Passing Arguments to a Function
Lecture 6 C++ Programming
Pointers and Arrays Chapter 12
Tejalal Choudhary “C Programming from Scratch” Pointers
Pointers and References
C Passing arrays to a Function
Chapter 10: Pointers 1.
Object Oriented Programming COP3330 / CGS5409
Chapter 9: Pointers.
Classes, Encapsulation, Methods and Constructors (Continued)
Pointers and Arrays Chapter 12
Initializing variables
Objectives You should be able to describe: Addresses and Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Overloading functions
Pointers Pointers point to memory locations
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Suggested self-checks: Section 7.11 #1-11
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
Exercise Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Lecture 14: Problems with Lots of Similar Data
Pointers and References
Introduction to Pointers
Presentation transcript:

Lecture 18 Arrays and Pointer Arithmetic C Programming Lecture 18 Arrays and Pointer Arithmetic

What is an Array? An array is a sequence of data items that are: all of the same type a sequence of integers, or a sequence of characters, or a sequence of floats, etc. indexible you can indicate or select the first element or second element or ... stored contiguously in memory each element of the sequence is stored in memory immediately after the previous element

Relationship Between Arrays and Pointers An array name represents the address of the beginning of the array in memory. When an array is declared, the compiler must allocate a base address and a sufficient amount of storage (RAM) to contain all the elements of the array. The base address of the array is the initial location in memory where the array is stored. It is the address of the first element (index 0) of the array.

Similarities of Arrays and Pointers Suppose we write the declaration #define N 100 int ary[N], *p; and the system causes memory bytes 300, 304, 308, . . ., 696 to be the address of ary[0], ary[1], ary[2], . . ., ary[99] with location 300 being the base address. Then The two statements p = ary; and p = &ary[0]; are equivalent.

Pointer Arithmetic Continuing with the previous example: p = ary + 1; and p = &ary[1]; are equivalent and would assign 304 (the address of the 2nd integer in the array) to p. Assuming the elements of ary have been assigned values, we could use the following code to sum the elements. sum = 0; for (p = ary; p < &ary[N]; ++p) sum += *p;

Three Ways to Sum the Elements of the Previous Array int ary[N], *p; . . . sum = 0; for (p = ary; p < &ary[N]; ++p) sum += *p; for (i = 0; i < N; ++i) sum += *(ary + i); sum += ary[i];

Differences Between Arrays and Pointers In many ways arrays and pointers can be treated alike. But there is one essential difference: Because the array name represents a constant pointer and not a variable, you cannot make an assignment to an array name or perform any operation that attempts to change the base address of the array. ary = p; ++ary; ary += 2 are all illegal expressions.

Pointer Arithmetic and Element Size If the variable p is a pointer to a particular type, then p + 1 yields the correct machine address for storing and accessing the next variable of that type. Expressions such as p + i and ++p and p+= i also are also valid and make sense. This means that pointer arithmetic does not behave exactly the same as other arithmetic expressions.

Example of Difference Between Expressions Using Pointer Arithmetic and the Usual Arithmetic Expressions int main(void) { double ary[2], *p, *q; p = &ary[0]; /* points at base address of ary */ q = p + 1; /* equivalent to q = &ary[1]; */ printf(“%d\n”, q - p); /* 1 is printed */ printf(“%d\n”, (int) q - (int) p); /* 8 is printed */ return(0); } Note: What is printed by the last statement is system- dependent. If a double is stored in 8 bytes on the machine where the code is being executed, then an 8 will be printed.

Passing Arrays to Functions In a function definition, a formal parameter that is declared as an array is actually a pointer. When an array is passed, its base address is passed call-by-value. The array elements themselves are not copied. As a notational convenience, the compiler allows array bracket notation to be used in declaring pointers as parameters.

Example of Passing an Array to a Function int sum(int a[], int n) /* n is size of the array */ { int i, s = 0; for (i = 0; i < n; ++i) s += a[i]; return s; } alternative function definition: int sum(int *a, int n) /* n is size of the array */ Notice -- no size here. That was provided in the original declaration of the array.

Equivalence of int a[]; and int *a; As part of the header of a function definition the declaration int a[]; is equivalent to int *a; Within the body of a function, these are not equivalent. The first will create a constant pointer (and no storage for an array). The second will create a pointer variable.

Various Ways sum[ ] Might be Called Invocation What gets computed and returned sum(v, 100) v[0] + v[1] + . . . + v[99] sum(v, 88) v[0] + v[1] + . . . + v[87] sum(&v[7], k - 7) v[7] + v[8] + . . . + v[k - 1] sum(v + 7, 2 * k) v[7] + v[8] + . . . + v[2 * k + 6]