Arrays: Part 2 10-18-2003. Opening Discussion zWhat did we talk about last class? zWhat do you think the picture of memory looks like when we declare.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Strings.
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.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Week 8 Arrays Part 2 String & Pointer
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
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.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2006 Pearson Education, Inc. All rights reserved Arrays.
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.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Microsoft Visual C++.NET Chapter 61 Memory Management.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
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.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
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”
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
More Inheritance and More Java Stuff Opening Discussion zWhat did we talk about last class? zHave you read the description of assignment #2?
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Engineering Computing I Chapter 5 Pointers and Arrays.
Characters and Strings
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
C Strings Doing strings the old fashioned way. strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Introduction to programming in java Lecture 21 Arrays – Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointers and Dynamic Arrays
Chapter 8 Arrays, Strings and Pointers
Chapter 7 Pointers and C-Strings
Motivation and Overview
© 2016 Pearson Education, Ltd. All rights reserved.
Student Book An Introduction
7 Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 16 Pointers and Arrays
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Arrays: Part

Opening Discussion zWhat did we talk about last class? zWhat do you think the picture of memory looks like when we declare an array? We saw last time that arrays are passed by reference. What does that imply? Does it change this picture?

Recap of Arrays zLast time we introduced arrays and saw that they gave us the ability to store, access, and manipulate numerous pieces of data of the same type using integer indexes. zArrays are denoted by the square brackets that follow a variable. When declaring an array the number in brackets gives the size, for use it gives the index, when passing arrays they are left empty.

Arrays in Memory zWhat we didn’t really talk about is what happens in memory when we declare an array. I said you get a chunk of memory large enough to hold the elements, but there is more. zThe variable for the array name is actually associated with a pointer that points to the block of memory of the proper size.

Array Syntax vs. Pointer Syntax  If int a[5]; actually declares a pointer to a block of memory, it is reasonable to ask how the [] notation works with that.  Just the name, a, is the pointer to the memory block. zAs it turns out, the brackets are equivalent to adding an offset and dereferencing. a[i] *(a+i)

Using const with Function Arguments zWe mentioned yesterday that there is a problem with arrays being passed by reference, mainly that we can mess them up if we aren’t careful. This can be avoided. Array and pointer arguments can be preceded with the const keyword which says they can’t be modified in the function. int sumArray(const int a[],int len);

Strings in C zLast class I mentioned that strings in C are null terminated arrays of characters. So strings can be treated/passed as arrays of chars or pointers to chars. zprintf and scanf can work with strings using the %s symbol. char buf[20]; scanf(“%s”,buf); printf(“%s\n”,buf);

Code zNow let’s write some code to explore the relationship between arrays and pointers and a bit about strings in C.

Minute Essay zIf you have a lower case letter, you can make it upper case by adding the value (‘A’-’a’). Given this, write a chunk of code the will make a char array, buf, all upper case. zQuiz #4 is next class. It will cover pointers and arrays. Assignment #5 is due on Monday.