Pointers (continued) Chapter 11

Slides:



Advertisements
Similar presentations
Pointers and Arrays Chapter 12
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
Chapter 12: Pointers and Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
C++ Review Data Structures.
Stack and Heap Memory Stack resident variables include:
Chapter 7: User-Defined Functions II
Computer Science 210 Computer Organization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
Strings (Continued) Chapter 13
Formatted Input/Output
Functions Dr. Sajib Datta
Module 5 Sorting and Searching: Bubble sort Selection sort
Pointers and Pointer-Based Strings
9. FUNCTIONS.
Pointers.
CNG 140 C Programming (Lecture set 10)
Command-Line Arguments
Formatted Input/Output
User-Defined Functions
Pointers and Arrays Chapter 12
Computer Science 210 Computer Organization
Scope, Parameter Passing, Storage Specifiers
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Lecture 18 Arrays and Pointer Arithmetic
Pointers Call-by-Reference CSCI 230
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Pointers and Arrays Chapter 12
Functions with arrays.
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
ECE 103 Engineering Programming Chapter 32 Array Parameters
Chapter 7: User-Defined Functions II
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Exam 2 Exam 2 Regrading Average: 69 TA: Fardad
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C Programming Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Pointers (continued) Chapter 11 Copyright © 2008 W. W. Norton & Company. All rights reserved.

Program: Finding the Largest and Smallest Elements in an Array The max_min.c program uses a function named max_min to find the largest and smallest elements in an array. Prototype for max_min: void max_min(int a[], int n, int *max, int *min); Example call of max_min: max_min(b, N, &big, &small); When max_min finds the largest element in b, it stores the value in big by assigning it to *max. max_min stores the smallest element of b in small by assigning it to *min. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Program: Finding the Largest and Smallest Elements in an Array max_min.c will read 10 numbers into an array, pass it to the max_min function, and print the results: Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31 Largest: 102 Smallest: 7 Copyright © 2008 W. W. Norton & Company. All rights reserved.

maxmin.c /* Finds the largest and smallest elements in an array */   /* Finds the largest and smallest elements in an array */ #include <stdio.h> #define N 10 void max_min(int a[], int n, int *max, int *min); int main(void) { int b[N], i, big, small; printf("Enter %d numbers: ", N); for (i = 0; i < N; i++) scanf("%d", &b[i]); Copyright © 2008 W. W. Norton & Company. All rights reserved.

max_min(b, N, &big, &small); printf("Largest: %d\n", big); printf("Smallest: %d\n", small); return 0; } void max_min(int a[], int n, int *max, int *min) { int i; *max = *min = a[0]; for (i = 1; i < n; i++) { if (a[i] > *max) *max = a[i]; else if (a[i] < *min) *min = a[i]; Copyright © 2008 W. W. Norton & Company. All rights reserved.

Using const to Protect Arguments When an argument is a pointer to a variable x, we normally assume that x will be modified: f(&x); It’s possible, though, that f merely needs to examine the value of x, not change it. The reason for the pointer might be efficiency: passing the value of a variable can waste time and space if the variable requires a large amount of storage. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Using const to Protect Arguments We can use const to document that a function won’t change an object whose address is passed to the function. const goes in the parameter’s declaration, just before the specification of its type: void f(const int *p) { *p = 0; /*** WRONG ***/ } Attempting to modify *p is an error that the compiler will detect. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Pointers as Return Values Functions are allowed to return pointers: int *max(int *a, int *b) { if (*a > *b) return a; else return b; } A call of the max function: int *p, i, j; … p = max(&i, &j); After the call, p points to either i or j. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Pointers as Return Values Although max returns one of the pointers passed to it as an argument, that’s not the only possibility. A function could also return a pointer to an external variable or to a static local variable. Never return a pointer to an automatic local variable: int *f(void) { int i; … return &i; } The variable i won’t exist after f returns. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Pointers as Return Values Pointers can point to array elements. If a is an array, then &a[i] is a pointer to element i of a. It’s sometimes useful for a function to return a pointer to one of the elements in an array. A function that returns a pointer to the middle element of a, assuming that a has n elements: int *find_middle(int a[], int n) { return &a[n/2]; } Copyright © 2008 W. W. Norton & Company. All rights reserved.