Arrays and Pointers (part 2) CSE 2031 Fall 2010 17 March 2016.

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
C Programming - Lecture 5
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Chubaka Producciones Presenta :.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Functions & Pointers in C Jordan Erenrich
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Engineering Computing I Chapter 5 Pointers and Arrays.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
1 Array. 2 Outline Aggregate scalar data into larger data Pointers vs. Array Array subscript and pointer arithmetic Memory layout for array –Static vs.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Dynamic memory allocation and Intraprogram Communication.
Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Command Line Arguments
Command Line Arguments
Programmazione I a.a. 2017/2018.
Pointers.
Arrays and Pointers CSE 2031 Fall September 2018.
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
14th September IIT Kanpur
Outline Defining and using Pointers Operations on pointers
McDonald’s calendar 2007.
7. Pointers, Dynamic Memory
Summary CSE 2031 Fall May 2019.
Arrays and Pointers (part 2)
Quick Review EECS May 2019.
Arrays and Pointers (part 2)
Characters and Strings
McDonald’s calendar 2007.
SPL – PS2 C++ Memory Handling.
2015 January February March April May June July August September
Introduction to Problem Solving and Programming
Presentation transcript:

Arrays and Pointers (part 2) CSE 2031 Fall March 2016

Be extra careful with pointers! Common errors: Overruns and underruns  Occurs when you reference a memory beyond what you allocated. Uninitialized pointers Null pointers de-referencing Memory leaks Inappropriate use of freed memory Inappropriately freed memory 2

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

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

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

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

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

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

char *words[] = { “apple”, “cherry”, “prune” }; char **p; p = words; printf("%c\n", **p); printf("%c\n",*(*(p+1)+2)); printf("%c\n",*(*(p+2)+2)+1); Arrays of Pointers: Example +1 9

Pointers to Whole Arrays char a[10] = “banana”; char *p1; char (*p2)[10]; p1 = a; /* p1 points to first element*/ p2 = &a; /* p2 points to the whole array */ printf(“%c %c”, *p1, **p2); // What’s the difference between p1+1 and p2+1 ? // What’s char *p3[100] ? 10

Pointers to Pointers (5.6) 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); bffff620 bffff61c a Output on some system: 11

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}}; int a[ ][ ] = { {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; 12

Multi-dimensional Arrays (cont.) Multi-dimensional arrays are arrays of arrays. For the previous example, a[0] is a pointer to the first row. Lay out in memory a[0][0] a[0][1]a[0][2]a[1][0] a[1][1] 13 a[0]a[1]

Multi-dimensional Arrays: Example #include 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)); }

Multi-D Arrays in Function Declarations int a[2][13]; // to be passed to function f f( int daytab[2][13] ) {... } or f( int daytab[ ][13] ) {... } or f( int (*daytab)[13] ) {... } Note: Only to the first dimension (subscript) of an array is free; all the others have to be specified. 15

Initialization of Pointer Arrays (5.8) /* month_name: return name of n-th month */ char *month_name( int n ) { static char *name[] = { "Illegal month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; return (n 12) ? name[0] : name[n]; } 16

Pointers vs. Multi-D Arrays (5.9) int a[10][20]; int *b[10]; a: 200 int-size locations have been set aside. 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. Advantage of b: the rows of the array may be of different lengths (saving space). 17

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

Command-Line Arguments (5.10) 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. So argc is at least 1. argv[argc] is a NULL pointer. 19

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 20

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

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

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; } 23

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

Complicated Declarations (5.12) char **argv int (*daytab)[13] int *daytab[13] int *comp() argv: pointer to pointer to char daytab: pointer to array[13] of int daytab: array[13] of pointer to int comp: function returning pointer to int 25

Next time... Introduction to UNIX 26