INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
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.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 10.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Pointers *, &, array similarities, functions, sizeof.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
1 MT258 Computer Programming and Problem Solving Tutorial 03.
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.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
Windows Programming Lecture 03. Pointers and Arrays.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Array in C# Array in C# RIHS Arshad Khan
Pointers and Dynamic Arrays
INC 161 , CPE 100 Computer Programming
User-Written Functions
(Numerical Arrays of Multiple Dimensions)
Computer Science 210 Computer Organization
UNIT 5 C Pointers.
ECE Application Programming
INC 161 , CPE 100 Computer Programming
Computer Programming BCT 1113
Arrays Declarations CSCI N305
Lecture-5 Arrays.
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Module 2 Arrays and strings – example programs.
Arrays in C.
INC 161 , CPE 100 Computer Programming
CS 1430: Programming in C++.
Computer Science 210 Computer Organization
14th September IIT Kanpur
The C “switch” Statement
The C “switch” Statement
One-Dimensional Array Introduction Lesson xx
Instructor: Ioannis A. Vetsikas
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
CS-161 Computer Programming Lecture 14: Arrays I
EKT150 : Computer Programming
Arrays November 8, 2017.
Declaration, assignment & accessing
Lecture 18 Arrays and Pointer Arithmetic
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
prepared by Senem Kumova Metin modified by İlker Korkmaz
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Initializing variables
7 Arrays.
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Exercise Arrays.
Arrays.
The switch Statement Topics Multiple Selection switch Statement
C++ Array 1.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Week 7 - Monday CS 121.
Presentation transcript:

INC 161 , CPE 100 Computer Programming Lecture 12 Pointer (part 2)

Pass Arrays of Fixed Length to Function Pass one dimensional array When an array is passed to a function, what is actually passed is the address of the first element of the array. In calling function, specify array name without brackets type name[10]; …… function(name); Note that name is equivalent to a pointer The definition of called function return_type function(type name[]){ } The array_name in the function definition is not necessary the same as in the function call.

The size of the array can be either entered or omitted. return_type function(type name[]) { } Or return_type function(type name[10])

Output: Example 1: Function that add two arrays #include <stdio.h> void oneDadd(double dd1[], double dd2[5]) { int i; for(i = 0; i < 5; i++) { dd1[i] += dd2[i]; } main() { double d1[5] = {1, 2, 3, 4, 5}; double d2[5] = {1, 2, 3, 4, 5}; oneDadd(d1, d2); printf("%f ", d1[i]); printf(“\n"); Output: 2.00 4.00 6.00 8.00 10.00

Pass two-dimensional array In calling function, specify array name without brackets int a1[2][3], a2[2][3]; …… function(a1, a2); The definition of called function return_type function (int a1[2][3], int a2[][3]){ } The name in the function definition is not necessary the same as in the function call.

Example 2: Function that add two 2Darrays void twoDadd(double dd1[][3], double dd2[][3], double dd3[2][3]) { int i, j; for(i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { dd1[i][j] = dd2[i][j]+dd3[i][j]; } main() { double d1[2][3], d2[2][3]={{1,2,3},{4,5,6}}, d3[2][3]={{1,2,3},{4,5,6}}; twoDadd(d1, d2, d3); Input: d2 = 1.00 2.00 3.00 4.00 5.00 6.00 d3 = Output: d1 = 2.00 4.00 6.00 8.00 10.00 12.00

Using Pointers to Pass One-Dimensional Arrays to a Function When an array is passed to a function, what is actually passed is the address of the first element of the array. Array name = address of the first element of the array This is why pointers can be used to pass one-dimensional arrays to functions.

#include <stdio.h> double func(double aa[]) { double sum = 0; int i; for(i=0; i<5; i++) { sum += aa[i]; } return sum; main() { double a[5] = {1, 2, 3, 4, 5}; double sum; sum = func(a); printf("sum = %f\n", sum); #include <stdio.h> double func(double *aa) { double sum = 0; int i; for(i=0; i<5; i++) { sum += *(aa+i); } return sum; main() { double a[5] = {1, 2, 3, 4, 5}; double sum; sum = func(a); printf("sum = %f\n", sum);

The function definition double func(double *aa) can be replaced by double func(double aa[]) The statement sum += *(aa+i); sum += aa[i];

Pointers to Pointers A pointer to a pointer is defined as a variable which contains the address of another pointer. Consider the following code: int i; // i is an integer i = 10; int *p = &i; // p is a pointer pointes to i int **pp = &p; // pp pointes to p Here, **p refers to memory address of pointer p which refers to the memory address of variable i. It can be illustrated by the following figure:

Example: **pp == *(*pp) == *p == i > int i, *p > p = &i // assign address of i to p 00E81E20 > *p = 10 10 > printf(“i = %d\n”, i); i = 10 > int **pp > pp = &p // assign address of p to pp 00E82040 > printf(“**pp = %d\n”, **pp) **pp = 10 **pp == *(*pp) == *p == i

If the address of i is 0x00E81E20, the value of p is 0x00E81E20 as shown below. Assume pp has address of 0x00E83060.

Array of Pointers Since pointers are also variables, arrays of pointers are supported. Array of pointers can store indexes to multiple strings Below is an example of how arrays of pointers can be declared and used: char *s1 = “abc”; char *s2 = “123”; char *p[3]; //declares an array of 3 pointers to char; p[0] = s1; p[1] = s2; p[2] = NULL;

Multiple strings can also be stored using 2D Array of char char day1[7][10] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};

Array of pointers to char char *day2[7] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}; The expression day2[1][3] has the value of ‘d’ of “Monday”. The advantage of using day2, the array of pointers, is that each pointer can point to arrays with different length rather than the fixed length of 10 bytes.

Output: Use NULL to indicate the end of array of strings. Example: A list of names terminated by NULL. char *p[] = {“John", “Doe", “Peter”, NULL}; int i; for(i=0; p[i]!=NULL; i++) { printf(“p[i] = %s\n”, p[i]); } Output: John Doe Peter

char *p[3] = {"ABC", "HIJKL", "EF"}; The following example demonstrates how an array of pointers can be used to eliminate complicated storage management and overheads of moving lines. Example: char *p[3] = {"ABC", "HIJKL", "EF"}; char *tmp; ... tmp = p[1]; p[1] = p[2]; p[2] = tmp; (Before swapping texts.) (After swapping texts.)

Pointer to Functions A function pointer is a variable containing the address of the function. A function’s address is the entry point of the function. So, a function’s pointer can be used to call a function. A function pointer can be assigned, placed in arrays, passed to functions, returned by functions, etc. Below are some declarations of function pointers. void (*pf1) (void); int (*pf2) (); int (*pf3) (double f); Similar to an array name, a function name also represents for the address of a function. However, the address operator ‘&’ is ignored. For example, the statement pf = &fun; is treated as pf = fun;

Example: Call function by using function’s pointer #include <stdio.h> int fun(double f) { printf("f = %f\n", f); return 0; } main() { int (*pf)(double f); fun(10.0); pf = fun; // fun = address of the function pf(20.0); // call function fun by calling pf Output: f = 10.000000 f = 20.000000