Arrays and Pointers. CSE 2031 Fall November 11, 2013

Similar documents
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)

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!

CSE2301. Arrays. Arrays. Arrays and Pointers

Arrays, Pointers and Memory Management

CSE2301. Arrays. Arrays. Arrays and Pointers

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

Dynamic memory allocation (malloc)

Quick review pointer basics (KR ch )

Course organization. Course introduction ( Week 1)

Summer May 18, 2010

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

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

Multidimension array, array of strings

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

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Pointers. Pointer References

Content. In this chapter, you will learn:

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Pointers. Pointers. Pointers (cont) CS 217

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

SYSC 2006 C Winter 2012

Character Strings. String-copy Example

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

Memory Allocation in C

Array Initialization

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

[0569] p 0318 garbage

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

Fundamental of Programming (C)

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Computer Programming Unit 3

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

Arrays, Strings, and Pointers

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

Procedural programming with C

Personal SE. Functions, Arrays, Strings & Command Line Arguments

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

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

EM108 Software Development for Engineers

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

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

CS201- Introduction to Programming Current Quizzes

Arrays, Strings, and Pointers

Intermediate Programming, Spring 2017*

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

Dynamic Memory Allocation

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

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

CS 61c: Great Ideas in Computer Architecture

Dynamic memory allocation

CS 241 Data Organization Pointers and Arrays

POINTER AND ARRAY SUNU WIBIRAMA

In Java we have the keyword null, which is the value of an uninitialized reference type

K. N. King: chapters 8 and 12

PDS Class Test 2. Room Sections No of students

Arrays and Pointers in C. Alan L. Cox

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

ESC101N: Fundamentals of Computing End-sem st semester

Dynamic Allocation in C

... Lecture 12. Pointers

Introduction to Scientific Computing and Problem Solving

Arrays, Strings, and Pointers

Understanding Pointers

Pointers and File Handling

Chapter 7 C Pointers

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

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

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Variation of Pointers

int marks[10]; // fixed size and fixed address No change in Memory address.

Arrays and Memory Management

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

ESc101: Pointers and Arrays

Kurt Schmidt. October 30, 2018

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

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

Dynamic Allocation in C

Computer Science & Engineering 150A Problem Solving Using Computers

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

C PROGRAMMING Lecture 5. 1st semester

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Basic and Practice in Programming Lab7

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

High Performance Programming Programming in C part 1

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

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

Homework #3 CS2255 Fall 2012

CS61C : Machine Structures

Transcription:

Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1

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

Arrays: Example l Syntax type name[size]; l Examples int bigarray[10]; double a[3]; char grade[10], onegrade; 3

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

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

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 6

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

Multi-dimensional Arrays (5.7) 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}}; To access the elements: if ( a[2][0] == 7 ) printf (... ); for ( i=0, j=0;... ; i++, j++ ) a[i][j] = i+j; int a[ ][ ] = { {1,2,3}, {4,5,6}, {7,8,9}}; 8

Multi-dimensional Arrays (cont.) l Multi-dimensional arrays are arrays of arrays. l Lay out in memory a[0] a[1] a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] 9

Pointers CSE 2031 Fall 2013 10

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

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

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

Pointer Example 1 int x,y; int *z; x = 25; y = x; z = &x; x 25 1200-3 1204 7 1208-11 y z 25 1204 9608-11 8404-7 14

Pointer Variables z = 1024; BAD idea Instead, use z = &x 15

Pointer Example 2 int x = 25, *y, z; y = &x; z = *y; x 25 1200-3 1204 7 1208-11 y z 1204 25 9608-11 8404-7 16

Pointer Example 3 17

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

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

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); 20

Arrays and Pointers 21

Pointers and Arrays (5.3) l Identifier of an array is equivalent to the address of its first element. int numbers[20]; int *p; p = numbers; numbers = p; // Valid // Invalid l p and numbers are equivalent and they have the same properties. l 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. 22

Pointers and Arrays: Example int a[10]; /* Init a[i] = i */ int *pa; pa = &a[0] x = *pa; /*same as x = a[0]*/ int y, z; y = *(pa + 1); z = *(pa + 2); 23

Pointers and Arrays: More Examples int a[10], *pa; pa = a; /* same as pa = &a[0]*/ pa++; /*same as pa = &a[1]*/ a[i] *(a+i) &a[i] a+i pa[i] *(pa+i) Notes a = pa; a++; are illegal. Think of a as a constant, not a modifiable variable. p[-1], p[-2], etc. are syntactically legal but best avoided. 24

Computing String Lengths /* strlen: return length of string s */ int strlen( char *s ) /* or (char s[]) */ { } int n; for ( n = 0; *s!= '\0 ; s++ ) n++; return n; Callers: strlen( "hello, world ); /* string constant */ strlen( array ); /* char array[100]; */ strlen( ptr ); /* char *ptr; pointing to an array */ 25

Passing Sub-arrays to Functions l It is possible to pass part of an array to a function, by passing a pointer to the beginning of the sub-array. my_func( int ar[ ] ) {...} or my_func( int *ar ) {...} my_func(&a[5]) or my_func(a + 5) 26

Address Arithmetic (5.4) Given pointers p and q of the same type and integer n, the following pointer operations are legal: l p + n, p n n is scaled according to the size of the objects p points to. If p points to an integer of 4 bytes, p + n advances by 4*n bytes. l q p, q p + 10, q p + n (assuming q > p) But p + q is illegal! l q = p; p = q + 100; If p and q point to different types, must cast first. Otherwise, the assignment is illegal! l if ( p == q ), if ( p!= q + n ) l p = NULL; l if ( p == NULL ), same as if (!p ) 27

Address Arithmetic: Example /* strlen: return length of string s */ int strlen(char *s) { } char *p = s; while (*p!= '\0') p++; return p - s; 28

Address Arithmetic: Summary l Legal: assignment of pointers of the same type adding or subtracting a pointer and an integer subtracting or comparing two pointers to members of the same array assigning or comparing to zero (NULL) l Illegal: add two pointers multiply or divide or shift or mask pointer variables add float or double to pointers assign a pointer of one type to a pointer of another type (except for void *) without a cast 29

Character Pointers and Functions (5.5) l A string constant ( hello world ) is an array of characters. l The array is terminated with the null character '\0' so that programs can find the end. char *pmessage; pmessage = "now is the time"; assigns to pmessage a pointer to the character array. This is not a string copy; only pointers are involved. C does not provide any operators for processing an entire string of characters as a unit. 30

Important Difference between... char amessage[] = "now is the time"; /* an array */ char *pmessage = "now is the time"; /* a pointer */ l amessage will always refer to the same storage. l pmessage may later be modified to point elsewhere. 31

Example: String Copy Function /* strcpy: copy t to s; array subscript version */ void strcpy(char *s, char *t) { } int i; i = 0; while ((s[i] = t[i])!= '\0') i++; /* strcpy: copy t to s; pointer version */ void strcpy(char *s, char *t) { } int i; i = 0; while ((*s = *t)!= '\0') { } s++; t++; /* strcpy: copy t to s; pointer version 2 */ void strcpy(char *s, char *t) { while ((*s++ = *t++)!= '\0') ; } 32

Dynamic Memory Allocation CSE 2031 Fall 2013 33

Dynamic Memory Allocation (7.8.5) How to allocate memory during run time? int x = 10; int my_array[ x ]; /* not allowed in C */ 34

malloc() l In stdlib.h void *malloc( int n ); l Allocates memory at run time. l Returns a pointer (to a void) to at least n bytes available. l Returns null if the memory was not allocated. l The allocated memory is not initialized. 35

calloc() void *calloc( int n, int s ); l Allocates an array of n elements where each element has size s; l calloc( ) initializes all the allocated memory to 0. 36

realloc() l What if we want our array to grow (or shrink)? void *realloc( void *ptr, int n ); l Resizes a previously allocated block of memory. l ptr must have been returned from a previous calloc, malloc, or realloc. l The new array may be moved if it cannot be extended in its current location. 37

free() void free( void *ptr ) l Releases the memory we previously allocated. l ptr must have been returned from a previous calloc, malloc, or realloc. l C does not do automatic garbage collection. 38

Example #include<stdio.h> #include<stdlib.h> int main() { int *a, i, n, sum = 0; printf( "Input an array size: " ); scanf( "%d", &n ); a = malloc ( n * sizeof(int) ); /* a = calloc( n, sizeof(int) ); */ for( i=0; i<n; i++ ) scanf( "%d", &a[i] ); for( i=0; i<n; i++ ) sum += a[i]; free( a ); printf("number of elements = %d\n", n); printf("sum is %d\n", sum); } 39

Be extra careful with pointers! Common errors: l Overruns and underruns Occurs when you reference a memory beyond what you allocated. l Uninitialized pointers l De-referencing null pointers l Memory leaks l Inappropriate use of freed memory 40

Uninitialized Pointers l Example 1 int *p; *p = 20; l Example 2 main() { char *x[10]; strcpy( x[1], Hello ); } 41

Null Pointer Dereferencing main( ) { int *x; } x = malloc( sizeof( int ) ); *x = 20; // What s wrong? Better code: x = malloc( sizeof( int ) ); if ( x == NULL ) { } printf( Insufficient memory!\n ); exit( 1 ); *x = 20; 42

Memory Leaks int *x; x = malloc( 20 ); x = malloc( 30 ); l The first memory block is lost for ever. l MAY cause problems (exhaust memory). 43

Inappropriate Use of Freed Memory char *x; x = malloc( 50 ); free( x ); x[0] = A ; /* Does work on some systems though */ 44

Arrays of Pointers (5.6) char *words[] = { apple, cherry, banana }; l words is an array of pointers to char. l Each element of words ( words[0], words[1], words[2]) is a pointer to char. words 0 1 2 apple cherry banana 45

Arrays vs. Pointers What is the difference between the previous example and the following? char words[][10] = { apple, cherry, banana }; Previous example supports rearrangement 46

Pointers to Pointers (5.6) l 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); Output on some system: -1073744352-1073744356 10-1073744352 -1073744356 10 bffff620 bffff61c a 47

Multi-dimensional Arrays: Example #include <stdio.h> int main() { float *pf; float m[][3]={{0.1, 0.2, 0.3}, {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)); } 36 0.4000 0.5000 0.6000 48

Pointers vs. Multi-D Arrays (5.9) int a[10][20]; int *b[10]; l a: 200 int-size locations have been set aside. l b: only 10 pointers are allocated and not initialized; initialization must be done explicitly. Assuming each element of b points to an array of 20 elements, total size = 200 integers + 10 pointers. l Advantage of b: the rows of the array may be of different lengths (saving space). 49

Advantage of Pointer Arrays char *name[ ] = { "Illegal month", "Jan", "Feb", "Mar" }; char aname[ ][15] = {"Illegal month", "Jan", "Feb", "Mar" }; 50

Command-Line Arguments (5.10) l Up to now, we defined main as main(). l Usually it is defined as main( int argc, char *argv[] ) l argc is the number of arguments. l argv is a pointer to the array containing the arguments. l argv[0] is a pointer to a string with the program name. So argc is at least 1. l argv[argc] is a NULL pointer. 51

Command-Line Arguments (cont.) 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 52

Example: Diagram l Write a program named echo (echo.c) which echoes its command-line arguments on a single line, separated by blanks. l Command: l Output: echo hello, world hello, world 53

echo, 1 st Version main( int argc, char *argv[] ) { int i; for (i = 1; i < argc; i++) printf("%s%s", argv[i], (i < argc-1)? " " : ""); printf("\n"); return 0; } 54

echo, 2 nd Version main( int argc, char *argv[] ) { while (--argc > 0) printf("%s%s", *++argv, (argc > 1)? " " : ""); printf("\n"); return 0; } 55