CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.

Slides:



Advertisements
Similar presentations
A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
Advertisements

Programming and Data Structure
Kernighan/Ritchie: Kelley/Pohl:
ICS103: Programming in C Searching, Sorting, 2D Arrays
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
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 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Pointers CSE 2451 Rong Shi.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
ICS103: Programming in C Searching, Sorting, 2D Arrays Muhamed F. Mudawar.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing 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]
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
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;
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
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.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
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.
CSE 220 – C Programming Pointers.
Programming application CC213
Hassan Khosravi / Geoffrey Tien
Array 9/8/2018.
Hassan Khosravi / Geoffrey Tien
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Lecture 10 Arrays.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
EKT150 : Computer Programming
CS111 Computer Programming
Outline Defining and using Pointers Operations on pointers
2-d Arrays.
Introduction to Problem Solving and Programming
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with 2D, 3D, multidimensional arrays (for storing matrices and other 2D or 3D data!)

CSE 251 Dr. Charles B. Owen Programming in C2 Review: Pointers Pointers are variables that store memory addresses int a = 15; int *b = &a; printf(“%x %x %d\n”, b, &b, *b); // prints effffa94 effffa98 15 AddressMemory 0xeffffa94 0xeffffa98 Name a b 15 0xeffffa94

CSE 251 Dr. Charles B. Owen Programming in C3 int number; int *ptr = &number; printf(“Enter an integer: ”); scanf(“%d”, &number); printf(“Enter another integer: “); scanf(“%d”, ptr); printf(“Number = %d, *ptr = %d\n”, number, *ptr); Using pointers in scanf function Read & as “at” Don’t have to put & before ptr. It’s already an “at”. Example output:

CSE 251 Dr. Charles B. Owen Programming in C4 int multiply( int *, int); int main() { int number = 3; int *ptr = &number; printf(“1: %d\n”, multiply( &number, 2 ) ); printf(“2: %d\n”, multiply( ptr, 3 ) ); } int multiply (int *a, int factor) { return (*a) * factor; } Passing pointers (addresses) to functions

CSE 251 Dr. Charles B. Owen Programming in C5 Review: Arrays An array is a contiguous chunk of memory to store multiple values int grades[]={74,59,95,85,71,45,99,82,76}; grades xeffffa000xeffffa040xeffffa080xeffffa0c0xeffffa100xeffffa140xeffffa18 0xeffffa1c 0xeffffa20 index A

CSE 251 Dr. Charles B. Owen Programming in C6 int sumArray( int [], int); int main() { int list[] = {1, 2, 3, 4}; printf(“Sum = %d\n”, sumArray( list, 4 )); } int sumArray (int list[], int arraySize) { int sumvalue = 0; for (int i=0; i<arraySize; i++) sumvalue += list[i]; return sumvalue; } Passing arrays to functions

CSE 251 Dr. Charles B. Owen Programming in C7 Array Name The array name is a pointer to the first element of the array 1234 [1][2][3][0] list ffe2de0cffe2de10ffe2de14ffe2de18 Output: ffe2de0c ffe2de0c 1 int list[]={1,2,3,4}; printf(“%x, %x, %d”, list, &list[0], *list);

CSE 251 Dr. Charles B. Owen Programming in C8 Pointers and Arrays int *p, int list[]={1,2,3,4}; p = list; /* equivalent to p = &list[0] */ printf(“%d\n”, *p); /* prints the value “1” */ 1234 [1][2][3][0] list p You can use a pointer to access the array

CSE 251 Dr. Charles B. Owen Programming in C9 Pointer and [] Any pointer to a block of memory can use the [] syntax, even if it is not declared as an array! 1234 [1][2][3][0] List P int *p, int list[]={1,2,3,4}; p = list; printf(“%d\n”, p[2]); // prints 3 int *v; and int v[]; /* Mean the same thing */

CSE 251 Dr. Charles B. Owen Programming in C10 Array indexing [] Indexing an array is just a way of finding a particular address in that block 1234 [1][2][3][0] list ffe2de0cffe2de10ffe2de14ffe2de18 list + 2 int list[] = {1,2,3,4}// array of 4 ints printf(“%d”, list[2]); This is equivalent to printf(“%d”,*(list+2)); *list – Contents pointed to by list *(list + 2) – Contents at list[2] B

CSE 251 Dr. Charles B. Owen Programming in C11 Pointer Arithmetic When we add to a pointer, such as (p + 1), we don’t literally add 1 to the pointer address Instead we add one “address” to the pointer

CSE 251 Dr. Charles B. Owen Programming in C12 Pointer Arithmetic p int list[] = {1, 2, 3, 4}; int *p = list; /* same as p = &list[0] */ printf(“%x”,p); /* prints ffe2de0c */ 1234 ffe2de0cffe2de10ffe2de14ffe2de18

CSE 251 Dr. Charles B. Owen Programming in C13 Pointer Arithmetic p int list[] = {1, 2, 3, 4}; int *p = list; /* same as p = &list[0] */ printf(“%x”,p); /* prints ffe2de0c */ p = p + 1; /* p increases by 4 */ printf(“%x”,p); /* prints ffe2de10 */ 1234 ffe2de0cffe2de10ffe2de14ffe2de18 Think of pointer arithmetic as add 1 “location” instead of one byte or address.

CSE 251 Dr. Charles B. Owen Programming in C14 Pointer Arithmetic p double list2[] = {1.0, 2.0, 3.0}; double *p = list2; /* same as p = &list2[0] */ printf(“%x”, p); /* prints ffe2de0c */ 1.0 ffe2de0cffe2de10ffe2de14ffe2de ffe2de1cffe2de20 C

CSE 251 Dr. Charles B. Owen Programming in C15 Pointer Arithmetic P double list2[] = {1.0, 2.0, 3.0}; double *p = list2; /* same as p = &list2[0] */ printf(“%x”,p); /* prints ffe2de0c */ p = p + 1; /* P increases by 8 bytes */ printf(“%x”,p); /* prints ffe2de14 */ 1.0 ffe2de0cffe2de10ffe2de14ffe2de ffe2de1cffe2de20

CSE 251 Dr. Charles B. Owen Programming in C16 Pointer Arithmetic on Arrays *(list+1) references the next element in the array (equivalent to list[1]) Be careful: *(++list) works too but now we have lost our pointer to the beginning of the array!!! – Equivalent to: list = list + 1; *list;

CSE 251 Dr. Charles B. Owen Programming in C17 sizeof() operator int i; int *ptr4i = &i; int IntArray[] = {1, 2, 3, 4, 5}; double j; double *ptr4j = &j; double doubleArray[] = {1.0, 2.0, 3.0, 4.0,5.0}; printf("Sizeof integer is %d bytes\n", sizeof(int)); printf("Sizeof double is %d bytes\n", sizeof(double)); printf("Sizeof i is %d bytes\n", sizeof(i)); printf("Sizeof pointer for i is %d bytes\n", sizeof(ptr4i)); printf("Sizeof j is %d bytes\n", sizeof(j)); printf("Sizeof pointer for j is %d bytes\n", sizeof(ptr4j)); printf("Sizeof intArray is %d bytes\n", sizeof(intArray)); printf("Sizeof doubleArray is %d bytes\n", sizeof(doubleArray)); Returns the number of bytes needed to store a variable or a data type

CSE 251 Dr. Charles B. Owen Programming in C18 sizeof() operator

CSE 251 Dr. Charles B. Owen Programming in C19 When we pass an array When we pass an array, we are passing the array address int sumArray( int [ ], int); int main() { int list[] = {1, 2,3, 4); … sumArray( list, 4 ); …

CSE 251 Dr. Charles B. Owen Programming in C20 When we pass an array This will work too (because array name is a pointer to the beginning of the array) int sumArray( int *, int); int main() { int list[] = {1, 2,3, 4); … sumArray( list, 4 ); …

CSE 251 Dr. Charles B. Owen Programming in C21 When we pass an array But this DOES NOT work! int sumArray( int *, int); int main() { int *list = {1, 2, 3, 4); … sumArray( list, 4 ); …

CSE 251 Dr. Charles B. Owen Programming in C22 Pointers 1 Indexing an array is just a way of finding a particular address in that block 1234 [1][2][3][0] list ffe2de0cffe2de10ffe2de14ffe2de18 list + 2 int list[] = {1,2,3,4}// array of 4 ints printf(“%d”, list[2]); This is equivalent to printf(“%d”,*(list+2)); *list – Contents pointed to by list *(list + 2) – Contents at list[2]

CSE 251 Dr. Charles B. Owen Programming in C23 2-D Arrays int cave[ArraySize][ArraySize]; Row Column

CSE 251 Dr. Charles B. Owen Programming in C24 2D Arrays int myMatrix[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} }; myMatrix[0][1]  2 myMatrix[2][3]  12 myMatrix[row][col] 0123 Column Row

CSE 251 Dr. Charles B. Owen Programming in C25 Physically, in one block of memory int myMatrix[2][4] = { {1,2,3,4},{5,6,7,8} }; Array elements are stored in row major order Row 1 first, followed by row2, row3, and so on ffe2de0cffe2de10ffe2de14ffe2de18ffe2de1cffe2de20ffe2de24ffe2de28 row 1row 2

CSE 251 Dr. Charles B. Owen Programming in C26 2D Array Name and Addresses myMatrix: pointer to the first element of the 2D array myMatrix[0]: pointer to the first row of the 2D array myMatrix[1]: pointer to the second row of the 2D array *myMatrix[1] is the address of element myMatrix[1][0] ffe2de0cffe2de10ffe2de14ffe2de18ffe2de1cffe2de20ffe2de24ffe2de28 int myMatrix[2][4] = { {1,2,3,4},{5,6,7,8} };

CSE 251 Dr. Charles B. Owen Programming in C27 Accessing 2D Array Elements int myMatrix[2][4] = { {1,2,3,4}, {5,6,7,8} }; Indexing: myMatrix[i][j] is same as *(myMatrix[i] + j) (*(myMatrix + i))[j] *((*(myMatrix + i)) + j) *(&myMatrix[0][0] + 4*i + j)

CSE 251 Dr. Charles B. Owen Programming in C28 Declaration #define ROWS 3 #define COLS 5 int table[ROWS][COLS]; void display (table);

CSE 251 Dr. Charles B. Owen Programming in C29 void display( int x[ROWS][COLS] ) { for (int i=0; i < ROWS; i++) { for (int j=0; j < COLS; j++ ) { printf(" x[%d][%d]: %d", i, j, x[i][j]); } printf("\n"); } printf("\n"); } 2D Arrays often require nested loops – two variables

CSE 251 Dr. Charles B. Owen Programming in C30 Table A = { {13, 22, 9, 23}, {17, 5, 24, 31, 55}, {4, 19, 29, 41, 61} }; Table B = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; ? ?

CSE 251 Dr. Charles B. Owen Programming in C31 passing 2d arrays In passing a multi-dimensional array, the first array size does not have to be specified. The second (and any subsequent) dimensions must be given! int myFun(int list[][10]);

CSE 251 Dr. Charles B. Owen Programming in C32 #define ROWS 3 #define COLS 5 int addMatrix( int [ ][COLS] ); int main() { int a[][COLS] = { {13, 22, 9, 23, 12}, {17, 5, 24, 31, 55}, {4, 19, 29, 41, 61} }; printf(“Sum = %d\n”, addMatrix( a ) ); } int addMatrix( int t[ ][COLS] ) { int i, j, sum = 0; for (i=0; i<ROWS; i++) for (j=0; j<COLS; j++) sum += t[i][j]; return sum; } 1