1 Arrays and Strings. 2 An array is a collection of variables of the same type that are referred to through a common name. It is a Data Structure which.

Slides:



Advertisements
Similar presentations
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
Advertisements

Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
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:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Chapter 10.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 8 Arrays and Strings
Pointers Applications
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Lecture 13 Static vs Dynamic Memory Allocation
Stack and Heap Memory Stack resident variables include:
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
ECE Application Programming
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
General comments [1]  array is a fixed sized group in which the elements are of the same type  The general form of array's definition is as follows:
Arrays and Pointers.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Module 2 Arrays and strings – example programs.
Programming Languages and Paradigms
14th September IIT Kanpur
EKT150 : Computer Programming
CS111 Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Arrays, Pointers, and Strings
Presentation transcript:

1 Arrays and Strings

2 An array is a collection of variables of the same type that are referred to through a common name. It is a Data Structure which can hold a set of data items of the same type(e.g., all the exam scores(integers) from test #1. –In C, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Arrays

3 Single-Dimension Arrays General form: type array_name[size]; The amount of storage required: total bytes = sizeof(type) × size of the array.

4 SIZE - must be positive constant integral expression: # define max 10 int x[max], y[max*2]; xy

5 For(i=0;i<=4;i++) a[i] = 7 + i* i; a * * * * * An element is accessed by subscripting the array name. This is done by placing the subscript of the element within square brackets after the name of the array.....

6 C has NO subscript checking on arrays. int count[10], i; /* this causes count to be overrun */ for(i=0; i<100; i++) count[i] = i; Memory will be accessed that has “garbage” values.

7 If you use an invalid subscript in an expression you will get unpredictable results: Total = count[25] * 24/6 + 9; There isn’t any count[25] array element that we have initialized. But it will be evaluated into a memory address and will use whatever value is represented in those bytes.

8 On the other hand if you use an invalid subscript in an assignment, you will destroy some undetermined portion of your program. Usually, but not always, your program will continue to run and either produce unpredictable results or abort. Count[50] = total * taxrate – penalty; Count[50] is an address that may be within The memory allocated for your program.

9 1-D Arrays Initialization General form type array_name[size] = {value_list}; int scores[5] = {60, 40, 90, 80, 60}; scores

10 1-D Unsized Array Initializations If the size of the array is not explicitly specified, the compiler automatically creates an array big enough to hold all the initializers present. This is called an unsized array. char e2[] = "Write error\n"; printf("%s has length %d\n", e2, sizeof (e2)); Size would be 13 (“\n” 1 char, plus the Null char “/0” 1 char.

11 Variable-Length Arrays You can declare an array whose dimensions are specified by any valid expression, including those whose value is known only at run time. This is called a variable-length array. Only local arrays. The Array “str” will be set as large as the argument passed to “dim”. void f(int dim) { char str[dim]; }

12 The fact that an: Array name is a memory address is Exploited in C. Since a pointer is a memory address, it follows that an array variable (name) is a pointer, in fact a pointer constant. Relationship Between Arrays & Pointers

13 When an array is declared, the compiler must allocate a base address and a sufficient amount of storage to contain all the elements of the array. Array name Base address ArrayName [0]Base Address

14 Difference between Pointer and Array Name A pointer variable is one that holds an address and can be changed to point to another object of the pointer’s type. An Array name is interpreted as the base address of the array which can not be changed. It is a Pointer constant.

15 Array Name as the Address char a[3]; a[0] a[1] a[2] address 1000 a address 1001 address 1002

16 int a[5]; a[0]=7; a[1]=8; a[2]=11; a[3]=16; a[4]=23; address 1000 a address 1004 address 1008 address 1012address if int is stored in four bytes.

17 #define n 100 int a[n], *p; p = a;p = &a[0]; same result A0Both Assign p the 1the Base Addr of 2 pthe Array Array Access Thru a Pointer

18 p a for (p = a; p < &a[n]; ++p) sum += *p; Increment Pointer P By 1 p = a;p = &a[0]; Address unit. Pointer p = a + 1;p = &a[1]; Arithmetic. p = a + 2;p = &a[2];......

19 #include Int main(void) { double a[2], *p, *q; p = &a[0]; q = p+1; // or q=&a[1]; printf(“%dn\”, q-p); //1 is printed printf(“%d\n”, (int)q-(int)p); //8 is printed return 0; } Page 311 q-p yields the int value representing the number of array elements between q and p. this is system dependent

20 Note:Because an Array name e.g., a is a constant pointer & not a variable, we can’t change the Addr of a. a = p;++a;a + = 2; ILLEGAL

21 Passing 1-D Arrays To Functions When an Array is Being passed its Base Address (array’s name without an index) is passed. The Array elements themselves are not copied. We are implementing ? Call by Value or Call by Reference.

22 int a[ ], intsum (int a[ ], int n) { Sums Arrays elements & returns Sum } a[ ] is actually a pointer. We are allowed use of [ ] to remind us it is an Array Address being passed in.

23 How to invoke the previous function. Function Calls : (Base Addr, # of elements being summed). sum(a, n) V Suppose V is an Array of SIZE 100 which exists & is Loaded. sum (v, 100) sum (v + 4, 50)v[4]…v[53] sum (&v[9], 60)v[9]…v[68] How could you write the Formal parameter receiving An array in a different manner ?

24 int main(void) { int i[10]; func1(i); /*... */ } pointer void func1(int *x) /* pointer */ { /*... */ } sized array void func1(int x[10]) /* sized array */ { /*... */ } unsized array void func1 (int x[]) /* unsized array */ { /*... */ }

25 Two- Dimensional Arrays A two-dimensional array is an array of one- dimensional arrays. int d[10][20]; The number of bytes of memory needed to hold it: Four bytes integer:10 × 20 ×4= 800 Bytes.

26 They are stored in a row-column matrix. left index row right indexcolumn. This means that the right index changes faster than the left when accessing the elements in the array in the order in which they are actually stored in memory.

27 ch[0][0] ch[0][1] ch[0][2] char ch[4][3]; ch[1][0] ch[1][1] ch[1][2] ch[2][0] ch[2][1] ch[2][2] ch[3][0] ch[3][1] ch[3][2] Right index determines column. Left index deter- mines row

28 2-D Arrays Initialization int sqrs[10] [2] = { 1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36, 7, 49, 8, 64, 9, 81, 10, 100 }; int sqrs[10] [2] = { {1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100} }; subaggregate grouping

29 2-D Unsized Array Initializations int sqrs[] [2] = { {1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 49}, {8, 64}, {9, 81}, {10, 100} }; int sqrs[] [2] = { 1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36, 7, 49, 8, 64, 9, 81, 10, 100 };

30 Passing 2-D Arrays To Functions When a two-dimensional array is used as an argument to a function, only a pointer to the first element is actually passed. However, the parameter receiving a two- dimensional array must define at least the size of the rightmost dimension. [2]) void func1(int x[] [2]) { /*... */ }

31 #include int main(void) { int t, i, num[3][4]; for(t=0; t<3; ++t) for(i=0; i<4; ++i) num[t][i] = (t*4)+i+1; /* now print them out */ for(t=0; t<3; ++t) { for(i=0; i<4; ++i) printf(''%3d ", num[t] [i]); printf("\n"); } return 0; } num[t][i] Nested loop. Why?

32 Mapping How an Array Address is Pg. 316 created. a a a[i][j] *(&a [0] [0] + 5 * i + j) a[2] [4] * The Row a # of column

33 Multi-Dimensional Arrays General form: type name[Size1][Size2][Size3]...[Sizen]; The number of bytes of memory needed to hold it: bytes = size of 1st index × size of 2nd index × …. × size of nth index sizeof(base type)

34 The first dimension is called the ‘Plane” of the array. int c [3] [5] [10] - 3 – dimensions 2 1 C

35 Passing M-D Arrays To Functions When passing multidimensional arrays into functions, you must declare all but the leftmost dimension. int m[4][3][6][5]; void func1(int d[][3][6][5]) { /*... */ }

36 Memory Allocation Dynamic Memory Allocation: Uses predefined functions to allocate and release memory for data while the program is running. Static Memory Allocation: Memory whose allocation is determined by the compiler and therefore preset before run time. Uses definitions and declarations in the source code. The number of bytes reserved can not be changed at run time.

37 Heap Memory allocated by C's dynamic allocation functions is obtained from the heap. The heap is free memory that is not used by your program, the operating system, or any other program running in the computer. The size of the heap cannot usually be known in advance, but it a typically contains a fairly large amount of free space. Although the size of the heap is often quite large, it is finite and can be exhausted.

38 Dynamic Allocation Functions calloc malloc Function prototypes: void * calloc(n, object_size) void * malloc(n * object_size) They are declared in the header. Return type void *: Can be assigned to any type of pointer without casting.

39 Dynamic Allocation Functions (cont.) Returns a pointer to enough space in memory to store n objects each of size bytes. N and size should be positive integers. If the system is unable to allocate the requested memory, the pointer value NULL is returned.

40 p = calloc (n, sizeof(int)); 10 p int2 Bytes Calloc is primarily used to allocate memory for arrays. 100

41 Dynamic Allocation Functions (cont.) Difference: –The storage set aside by calloc is automatically initialized to zero(blank if char). –The storage set aside by malloc is not initialized and therefore start with a garbage value. If efficiency is an issue, programmers prefer malloc instead of calloc.

42 Examples char *p; p = malloc(1000); /* get 1000 bytes */ /*p points to the first of 1,000 bytes of memory */ int *p; /* allocates space for 50 integers */ p = malloc(50*sizeof(int)); p = malloc(100); if(!p) { printf(''Out of memory.\n"); exit (1); }

43 Releasing Allocated Memory Function Space is NOT released on function exit. free It is declared in the header. It returns previously allocated memory to the system. Function prototype: void free(void *p); e.g. free(p);

44 Strings

45 String The most common array is the string, which is simply an one-dimensional array of characters terminated by a null. The End-of-String Sentinel (delimiter) \0. #define MAXWORD 100 int main(void){ char w[MAXWORD]; w[0] = ‘A’; w[1] = ‘\0’; /*….*/

46 When declaring a character array that will hold a string, you need to declare it to be one character longer than the largest string that it will hold. WHY? //to declare an array str that can hold a //10 characters string char str[11];

47 String Initialization General form: char array_name[size] = “string”; char str[9] = “I like C”; char str[9] = {'I', ' ', 'l', 'i', 'k', 'e',' ', 'C', '\0'}; char *p = “I like C”;

48 The format %s is used for a string. scanf(“%s”, w); //if w is a string var printf(“%s”, w); ‘a’ and “a” are very different. ‘a’a character constant “a”a array of characters with two elements.

49 String-Handling Functions Standard header Pg. 349 strcpy(s1, s2) Copies s2 into s1 strcat(s1, s2) Concatenates s2 onto the end of s1. strlen(s1) Returns the length of s1 strcmp(s1, s2) Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2

50 strchr(s1, ch) Returns a pointer to the first occurrence of ch in s1. strstr(s1, s2) Returns a pointer to the first occurrence of s2 in s1.

51 # include #include int main(void){ char s1[80], s2[80]; gets(s1); gets(s2); printf("lengths: %d %d\n", strlen(s1), strlen(s2)); if(!strcmp(s1, s2)) printf("The strings are equal\n"); Hello lengths: 5 5 The strings are equal

52 strcat(s1, s2); hellohello printf (''%s\n", s1); This is a test e is in hello found hi strcpy(s1, "This is a test.\n"); printf(s1); if(strchr("hello", 'e')) printf("e is in hello\n"); if(strstr("hi there", "hi")) printf("found hi"); return 0; }

53 Dynamically Allocated Strings The type pointer to char is conceptually a string. An example follows:

54 /* Allocate space for a string dynamically, request user input, and then print the string backwards. */ #include #include #include int main(void) { char *s; int t; s = malloc(80); if(!s) { printf(''Memory request failed.\n"); exit (1); } gets(s); for(t=strlen(s)-2; t>=0; t--) putchar(s[t]); free(s); return 0; }