Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.

Slides:



Advertisements
Similar presentations
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Lectures 10 & 11.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
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.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Introduction to C Programming CE
Programming Review: Functions, pointers and strings.
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",
Pointers and Arrays C and Data Structures Baojian Hua
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
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.
Pointers CSE 2451 Rong Shi.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
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.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
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”
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Functions & Pointers in C Jordan Erenrich
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
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;
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Pointer Tran, Van Hoai. Pointers and Addresses  Pointer: group of cells (2,4 cells) Variable: group of cells Pointer is also a variable  Each cell (or.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
C Primer.
Characters and Strings
Command Line Arguments
Command line arguments
Command Line Arguments
Checking Memory Management
Pointers.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Pointers.
Dynamic Memory A whole heap of fun….
Chapter 9: Pointers and String
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Functions Reasons Concepts Passing arguments to a function
Quick Review EECS May 2019.
15213 C Primer 17 September 2002.
Pointers.
Presentation transcript:

Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers and arrays of user-defined data types Next two weeks Memory allocation and dynamic structures Complex structures with pointers, structures, etc Pointers functions

Variable, memory storage and addresses Basic Pointer char short int ip1 ip2

Basic Pointer -- continued Pointers and references int i, *ip1, *ip2, a[8]; char c, *cp; ip1 = &i; ip2 = &a[0]; i = 8; ip1 = ip2; cp = &c;

Parameter Passing with Pointers Still Pass-by-value with pointers However the memory content can change Implications: Be aware of the argument type when passing parameters Be aware of the content is changing When you attempt to change pointer argument values, ask yourself twice, typically a no-no.

Parameter Passing -- Example void swap (int x, int y) { int temp; temp = x; x = y y = temp; } void swap (int *x, int *y) { int temp; temp = *x; *x = *y *y = temp; }

Pointers and Arrays Essentially, pointers and array identifier are memory addresses. Array name, in particular, is the address of the first element ip a: ip+1ip+2 ip[0]ip[1]ip[7] a[0]a[1]a[7]

Address Arithmetic Pointers can have arithmetic operations! int *ip1, *ip2, a[8]; ip1 = &a[7]; ip2 = &a[0]; Then: ip2 – ip1 + 1? ip1 - ip2 ? ip1 == NULL? ip1 > ip2 ?

Pointer Arrays Comparisons with multi-dimensional arrays A typical usage, command line arguments int *ip[3], a[3][4], i=0; ip[0] = &a[0][0]; ip[1] = &a[1][0]; ip[2] = &a[2][0];

Sample Usage -- command-line arguments int main (int argc, char * argv[]) { int i=0; while (i++< argc) { printf(“%s\n”, argv[i]); }