Arrays and Pointers CSE 2031 Fall 2010 12 July 2019.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to C Programming
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Kernighan/Ritchie: Kelley/Pohl:
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
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 CS 201 Passing Function as Parameter & Array Debzani Deb.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Pointers CSE 2451 Rong Shi.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Review 1 List Data Structure List operations List Implementation Array Linked List.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Array in C# Array in C# RIHS Arshad Khan
Arrays Chapter 7.
CSE 220 – C Programming 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
Array, Strings and Vectors
Pointers and Pointer-Based Strings
INC 161 , CPE 100 Computer Programming
CSC 172 DATA STRUCTURES.
Arrays in C.
Arrays and Pointers CSE 2031 Fall September 2018.
C++ Arrays.
INC 161 , CPE 100 Computer Programming
14th September IIT Kanpur
Array Data Structure Chapter 6
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Arrays November 8, 2017.
Programming in C Pointer Basics.
Review of Arrays and Pointers
prepared by Senem Kumova Metin modified by İlker Korkmaz
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Simulating Reference Parameters in C
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.
7 Arrays.
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
Arrays in Java.
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
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.
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Lecture 03 & 04 Method and Arrays Jaeki Song.
C++ Array 1.
Arrays and Pointers CSE 2031 Fall May 2019.
Programming Arrays.
Presentation transcript:

Arrays and Pointers CSE 2031 Fall 2010 12 July 2019

Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly.

Arrays Syntax Examples type name[size]; int bigArray[10]; double a[3]; char grade[10], oneGrade;

Arrays Defining an array: allocates memory int score[5]; Allocates an array of 5 integers named "score" Individual parts can be called: Indexed or subscripted variables "Elements" of the array Value in brackets called index or subscript Numbered from 0 to (size – 1)

Arrays Stored in Memory 1234 1235 1236 1237 1238 ….. … 1260 1261 a[0] a[1] a[2] a[n] Some other variables

Initialization In declarations enclosed in curly braces Declares array a and initializes first two elements and all remaining set to zero int a[5] = {11,22}; int b[ ] = {1,2,8,9,5}; Declares array b and initializes all elements and sets the length of the array to 5

Array Access x = ar[2]; ar[3] = 2.7; What is the difference between ar[i]++, ar[i++], ar[++i] ?

Strings No string type in C String = array of char char gretings[ ] = “Hello” H e l l o \0

Pointers CSE 2031 Fall 2010

Pointers and Addresses (5.1) Memory address of a variable Declared with data type, * and identifier type * pointer_var1, * pointer_var2, … Example. double * p; int *p1, *p2; There has to be a * before EACH of the pointer variables

Pointers and Addresses (cont.) Use the "address of" operator (&) General form: pointer_variable = &ordinary_variable Name of the pointer Name of ordinary variable

Using a Pointer Variable Can be used to access a value Unary operator * used * pointer_variable In executable statement, indicates value Example int *p1, v1; v1 = 0; p1 = &v1; *p1 = 42; printf(“%d\n“,v1); printf(“%d\n,*p1); Output: 42 42

Pointer Variables int x,y; int * z; x = 25; y = x; z = &x; 1200 1204 1208 25 25 1204 9608 8404

Pointer Variables (cont.) z= 1024 BAD idea Instead, use z = &x

Pointer Types int x = 25, *y, z; y = &x; z = *y; y 8404 1204 1200 1204 1208 25 25 9608 z

Another Example of Pointers int *p1, *p2, x = 8, y = 9; p1 = &x; p2 = &y;

More Examples int x = 1, y = 2, z[10], k; int *ip; ip = &x; /* ip points to x*/ y = *ip; /* y is now 1 */ *ip = 0; /* x is now 0 */ z[0] = 0; ip = &z[0]; /* ip points to z[0] */ for (k = 0; k < 10; k++) z[k] = *ip + k; *ip = *ip + 100; ++*ip; (*ip)++; /* How about *ip++ ??? */

Pointers and Function Arguments (5.2) Write a function that swaps the contents of two integers a and b. void main( ) { int a, b; /* Input a and b */ swap(a, b); printf(“%d %d”, a, b); { C passes arguments to functions by values. void swap(int x, int y) { int temp; temp = x; x = y; y = temp; }

The Correct Version void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } void main( ) { int a, b; /* Input a and b */ swap(&a, &b); printf(“%d %d”, a, b);

Next time ... Pointers, part 2 (Chapter 5)