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

Summer May 18, 2010

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)

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

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

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

Arrays, Pointers and Memory Management

Array Initialization

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

Pointers. Pointers. Pointers (cont) CS 217

CSC 2400: Computer Systems. Arrays and Strings in C

Pointers. Pointer References

COMP 2355 Introduction to Systems Programming

CSC 2400: Computer Systems. Arrays and Strings in C

ONE DIMENSIONAL ARRAYS

ARRAYS(II Unit Part II)

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

8. Characters, Strings and Files

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

Dynamic memory allocation (malloc)

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

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];

Computers Programming Course 11. Iulian Năstac

Computers Programming Course 10. Iulian Năstac

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

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

Course organization. Course introduction ( Week 1)

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

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

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

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

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

Introduction to Programming Block Tutorial C/C++

Multiple Choice Questions ( 1 mark)

C programming basics T3-1 -

CSE 333 Lecture 7 - final C details

Memory, Arrays & Pointers

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

Introduction to Computer Science Midterm 3 Fall, Points

just a ((somewhat) safer) dialect.

... Lecture 12. Pointers

Model Viva Questions for Programming in C lab

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

Pointers, Arrays, and Strings. CS449 Spring 2016

Computers Programming Course 12. Iulian Năstac

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

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Chapter 9. Pointers and Dynamic Arrays

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Dynamic arrays / C Strings

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Chapter 11 Introduction to Programming in C

CSE2301. Functions. Functions and Compiler Directives

Module 6: Array in C

Quick review pointer basics (KR ch )

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

Arrays, Pointers, and Strings

Arrays, Strings, and Pointers

Arrays, Strings, and Pointers

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

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

York University Faculty Science and Engineering Fall 2008

Lecture 3: C Programm

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

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

Fundamental of Programming (C)

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

Chapter 11 Introduction to Programming in C

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Arrays, Strings, & Pointers

Review of the C Programming Language for Principles of Operating Systems

CSE 303 Lecture 8. Intro to C programming

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

PRINCIPLES OF OPERATING SYSTEMS

211: Computer Architecture Summer 2016

Overview (1A) Young Won Lim 9/25/17

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Principles of C and Memory Management

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Computer Programming: Skills & Concepts (CP) Strings

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

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Arrays and Pointers in C. Alan L. Cox

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Chapter 11 Introduction to Programming in C

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Understanding Pointers

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

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

Programming in C. Pointers and Arrays

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 gretings[]= hello H e l l o \n 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 3

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 x = 25; y = x; z = &x; int x,y; int * z; 1200 1204 1208 25 25 1204 9608 8404 4

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 Pointers 5

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 *? 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) 6

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. String Functions Man the following functions strcpy strcmp strcat trlen strchr strstr 7

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] 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++)); } 8

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 Arrays vs. Pointers What is the difference between the last example and char words[][10] = { apple, cherry, banana }; 9

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++ } } strcpy void strcpy(char *s, char *t) { while( (*s++ = *t++)!= \0 ) ; } 10

EX char *words[] = { apple, cherry, banana }; Char **p; p=words; printf("%c\n", **p); printf("%c\n",*(*(p+1)+2)); printf("%c\n",*(*(p+2)+2)+1); a e o a e o +1 Pointers to Whole Arrays Char (*p2)[100]; char name[100]; char *p1; p1=name; p2=name; // What is the difference? Consider p1+1 and p2+1 Command-Line Arguments Up to now, we defines main as main() Usually it is defined as main(int argc, char*argv[]) argc is the number of arguments argv is a pointer to the array containing the arguments. argv[0] is a pointer to a string with the program name 11

Command-Line Arguments main(int argc, char *argv[]) { int i; printf( Number of arg=%d\n,argc); for(i=0;i<argc,i++) printf(%s\n,argv[i]); } a.out Number of arg=1 a.out a.out hi by 3 Number of arg=4 a.out hi by 3 What if./a.out Pointers to Functions Although functions are not variables, it is posible to assign a pointer to a function. That pointer could be manipulated, assigned, placed on arrays, or passed/returned to/by functions. int (*comp)(void *, void *) comp is a pointer to a function that has 2 void * arguments and returns an int int *comp(void *, void *) comp is a function that has 2 void * arguments and returns an *int 12

Example main() { float y,z; float (*myfun)(float); x=0.76; y=acosf(x); printf("%f\n",y); printf("===========\n"); myfun=acosf; z=myfun(x); printf("%f\n",z); } 0.707483 ========== 0.707843 Complicated declaration int *f( ); f returns a pointer to int int (*pf)() pf is a pointer to a function that returns int cha **argv argv is a pointer to pointer to char int (*daytab)[13] daytab pointer to an array [13] of int Complicated Declaration char (*(*x())[]) ( ) x is a function returning pointer to array of pointers to function returning char char (*(*x[3]) ( ) ) [5] x is an array[3] of pointer to function returning pointer to array[5] of char 13