CSE2301. Arrays. Arrays. Arrays and Pointers

Similar documents
CSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays

CSE2301. Arrays. Arrays. Arrays and Pointers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers (part 1)

Arrays and Pointers (part 1)

Summer May 18, 2010

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays, Pointers and Memory Management

ONE DIMENSIONAL ARRAYS

ARRAYS(II Unit Part II)

Computers Programming Course 11. Iulian Năstac

Multidimension array, array of strings

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Computers Programming Course 10. Iulian Năstac

Memory, Arrays & Pointers

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Programming. Pointers, Multi-dimensional Arrays and Memory Management

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Array Initialization

Basic and Practice in Programming Lab7

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

COMP 2355 Introduction to Systems Programming

Computer Programming: Skills & Concepts (CP) Strings

Computers Programming Course 12. Iulian Năstac

Introduction to Scientific Computing and Problem Solving

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

8. Characters, Strings and Files

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Language comparison. C has pointers. Java has references. C++ has pointers and references

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Introduction to Computer Science Midterm 3 Fall, Points

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Pointers. Pointers. Pointers (cont) CS 217

... Lecture 12. Pointers

C-LANGUAGE CURRICULAM

Pointers. Pointer References

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Computer Science & Engineering 150A Problem Solving Using Computers

Pointers, Arrays, and Strings. CS449 Spring 2016

Chapter 9. Pointers and Dynamic Arrays

Dynamic memory allocation (malloc)

CS201- Introduction to Programming Current Quizzes

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Module 6: Array in C

Arrays and Strings. Arash Rafiey. September 12, 2017

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Multiple Choice Questions ( 1 mark)

Chapter 7 Functions. Now consider a more advanced example:

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Programming for Engineers Arrays

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Contents. A Review of C language. Visual C Visual C++ 6.0

Week 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Review of Important Topics in CS1600. Functions Arrays C-strings

CS 61c: Great Ideas in Computer Architecture

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

EM108 Software Development for Engineers

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Technical Questions. Q 1) What are the key features in C programming language?

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Procedural Programming & Fundamentals of Programming

Create a Program in C (Last Class)

Principles of Programming. Chapter 6: Arrays

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Model Viva Questions for Programming in C lab

First of all, it is a variable, just like other variables you studied

Chapter 2: Basic Elements of C++

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

C Programming Multiple. Choice

Fundamentals of Programming Session 19

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Transcription:

Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who did CSE2301 Arrays and Pointers These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University Arrays Data structure Grouping of data of the same type Indicated with brackets containing positive integer constant or expression following identifier Subscript or index Loops commonly used for manipulation Programmer sets size of array explicitly Arrays Syntax type name[value]; Example Int bigarray[10]; Double a[3]; Char grade[10], onegrade; 1

Arrays Declare the array allocates memory int score[5]; Declares array of 5 integers named "score" Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] Individual parts called many things: Indexed or subscripted variables "Elements" of the array Value in brackets called index or subscript Numbered from 0 to size - 1 Arrays 1234 1235 1236 1237 1238.. a[0] a[1] a[2] a[n] 1260 1261 Some other variables Initialization In declarations enclosed in curly braces int a[5] = {11,22}; Declares array a and initializes first two elements and all remaining set to zero int b[ ] = {1,2,8,9,5}; Declares array b and initializes all elements and sets the length of the array to 5 2

Array Access X=ar[2]; ar[3]=2.7; What is the differenc ebetween ar[i]++, ar[i++], ar[++i]; Strings No string type in C Char greetings[]= hello H e l l o \n Array Declaration #define N_COL 200 const int N_ROW = 100; float arr[ N_ROW ][ N_COL ]; In C99 scanf( "%d", &N ) double data[ N ]; 3

Pointers 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 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 Output: 42 int *p1, v1; 42 v1 = 0; p1 = &v1; *p1 = 42; printf( %d\n,v1); printf( %d\n,*p1); what about p1? 4

Pointer Variables x = 25; y = x; z = &x; x int x,y; int * z; 1200 1204 1208 25 25 1204 9608 8404 Pointer variables z= 0x12345A BAD idea Instead, use z=& another-vriable Pointer Types y 8404 1204 z=*y 1200 1204 1208 25 25 9608 z 5

Pointers Pointers identifier of an array is equivalent to the address of its first element int numbers [20]; int * p; p = numbers numbers = p // Valid // Invalid p and numbers are equivalent and they have the same properties Only difference is that we could assign another value to the pointer p whereas numbers will always point to the first of the 20 integer numbers of type int Pointer Arithmetic int *x, *y int z; Can do z=x-y; x=null; if(c==null) Also, what is void *? 6

Pointer Arithmetic int x[10]; what is x[i] is it the same as *(x+i) What is the unit of x++ or x+5 5 what? Two functions void swap(int x, int y) void swap(int *x, int *y) Pointers void * (pointer to a void) is the generic pointer replacing char *) Legal: add/sub a pointer and an integer, subtracting and comparing 2 pointers to members of the same array, and assigning or comparing to zero. Illegal add, multiply or divide 2 pointers, or assign one type to another type except void * without a cast. Any pointer can be cast to void * and back again without loss of information (used for pointer argument). Functions Arrays passed to a functions are passed by reference. The name of the array is a pointer to its first element strcpy(char dest[], char src[]); Note that does not copy the array in the function call, just a reference to it. 7

String Functions Man the following functions strcpy strcmp strcat trlen strchr strstr Multi-Dimensional Arrays Int a[3][3]; Int a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9}}; Int a[ ][3] = { {1,2,3}, {4,5,6}, {7,8,9}}; Int a[ ][ ] = { {1,2,3}, {4,5,6}, {7,8,9}}; Multi-Dimensional Arrays Multi-dimensional arrays are array of arrys For the previous example, m[0] is a pointer to the first row. Lay out in memory M[0][0] M[0][1] M[0][2] M[1][0] 8

Multidimensional arrays #include <stdio.h> int main() { 36 float *pf; 0.4000 0.5000 0.6000 float m[][3]={ {0.1, 0.2, 0.3}, 0.6000 0.5000 0.4000 {0.4, 0.5, 0.6}, {0.7, 0.8, 0.9} }; printf("%d \n",sizeof(m)); pf=m[1]; printf("%f %f %f \n",*pf, *(pf+1), *(pf+2)); printf("%f %f %f \n",*pf, *(pf++), *(pf++)); } Array of Pointers Char *words[]={ apple, cherry, banana }; Words is an array of pointers to a char, each element of words words[0], is a pointer to a char. words 0 1 2 apple cherry banana Pointers to Pointers Pointers can point to integers, floats, chars, and other pointers. int **j; int *i; int k=10; i=&k; j=&i; printf( %d %d %d\n,j,i,k); printf( %d %d %d\n,j,*j,**j); printf( %x %x %x\n,j,*j,**j); On my system -1073744352-1073744356 10-1073744352 -1073744356 10 bffff620 bffff61c a 9

Arrays vs. Pointers What is the difference between the last example and char words[][10] = { apple, cherry, banana }; strcpy void strcpy(char *s, char *t) { int i; i=0; while( (s[i] = t[i])!= \0 ) i++; } strcpy void strcpy(char *s, char *t) { while( (*s = *t)!= \0 ) { s++; t++ } } 10

strcpy void strcpy(char *s, char *t) { while( (*s++ = *t++)!= \0 ) ; } EX char *words[] = { apple, cherry, banana }; Char **p; p=words; printf("%c\n", **p); a a e o e o +1 printf("%c\n",*(*(p+1)+2)); printf("%c\n",*(*(p+2)+2)+1); 11