Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.

Slides:



Advertisements
Similar presentations
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Advertisements

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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Pointers CSE 2451 Rong Shi.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
General comments [1]  array is a fixed sized group in which the elements are of the same type  The general form of array's definition is as follows:
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Windows Programming Lecture 03. Pointers and Arrays.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Computer Programming BCT 1113
Hassan Khosravi / Geoffrey Tien
Lecture-5 Arrays.
Hassan Khosravi / Geoffrey Tien
Module 2 Arrays and strings – example programs.
7 Arrays.
7 Arrays.
Arrays Arrays A few types Structures of related data items
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
Presentation transcript:

Arrays, Strings, and Pointers CSE 2451 Rong Shi

Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int rgb[3]; – double percents[10]; – char name[20]; – int s = 4; int trythis[s]; // not valid for most compilers elements with an index/subscript number from 0 to -1

Indexing Dijkstra’s argument for zero index Address of an array is the same as its first element – min = address of min[0]

Initializing Arrays Initialize entire array using values enclosed in braces – Ex: int myArray[5] = {1,2,3,4,5}; int myArray[5] = {1,2};// valid int myArray[5] = {1,2,3,4,5,6};// invalid Initialize individual array locations – Ex: int myArray[2]; myArray[0]=14; myArray[1]=4; Array location acts like a single variable – Ex: x = myArray[0]; myArray[1]=x+y;

Array vs. Pointer Array name is a pointer constant inta[10]; intb[10]; int*c; … c = &a[0];// c points to a[0] c = a;// equivalent b = a;// invalid a = c;// invalid

Copy an array #include int main() { int iMarks[4] = {78, 64, 66, 74}; int newMarks[4]; int i,j; for(i=0; i<4; i++) newMarks[i]=iMarks[i]; for(j=0; j<4; j++) printf("%d\n", newMarks[j]); return 0; }

Shallow and Deep copying

More syntax examples intarray[10]; int*ap = array + 2; // ap points at &array[2] 1. ap 2.*ap 3.ap[0] 4.ap *ap *(ap + 6) 7.ap[6] 8.&ap 9.ap[-1] 10.ap[9]

Index checking Index access is not checked by the compiler – Check for valid range manually – Especially important for user entered indices int array[2] = {0,0}; int input; scanf(“%d”, &input); array[input];// what could possibly go wrong? Attempt to access memory outside of range allocated by OS – Segmentation fault – Access OS memory

Character arrays – strings Character array terminated with the null byte Declaration – char arr[] = {'c','o','d','e','\0'}; – char arr[] = "code"; Array size is not specified in [] Logical size of the array is one more than the number of characters in the string literal – one extra for NUL byte (i.e. the \0 character) Static allocation – size of the array is determined at compile-time

Pointers and Strings In C our string type is an array of characters Array names are pointer constants Use the same syntax char *ptr; char str[40]; ptr = str;

Arrays as function parameters Prototypes: intstrlen( char *string ); intstrlen( char string[] ); Logically, a pointer to the first element is passed copy-by-value (See array operations examples)

Multi-dimensional arrays Declarations – [row][col] subscript order int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } – Values is an array of 3 elements, each an array of 4 elements Stored in row order

Backup

Another function topic – default arguments int f1(int arg1, double arg2, char* name, char *opt); int f2(int arg1, double arg2, char* name) { return f1(arg1, arg2, name, "Some option"); }