CSSE 332 Functions, Pointers in C. 2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization.

Slides:



Advertisements
Similar presentations
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Surviving C and PostgreSQL CS186 Supplemental Session 9/13/04 Matt Denny, Paul Huang, Murali Rangan (Originally prepared by Shariq Rizvi, Wei Xu, Shawn.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Informationsteknologi Monday, September 10, 2007Computer Systems/Operating Systems - Class 31 Today’s class Review of more C Operating system overview.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
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.
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.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
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.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
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;
1 2/2/05CS250 Introduction to Computer Science II Pointers.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Intro to Pointers in C CSSE 332 Operating Systems
Stack and Heap Memory Stack resident variables include:
C Primer.
Day 03 Introduction to C.
Pointers in C.
Day 03 Introduction to C.
C programming language
INC 161 , CPE 100 Computer Programming
Outline Defining and using Pointers Operations on pointers
Programming in C Pointers and Arrays.
Pointers and dynamic objects
Functions Reasons Concepts Passing arguments to a function
Functions By Anand George.
Pointers and pointer applications
15213 C Primer 17 September 2002.
Presentation transcript:

CSSE 332 Functions, Pointers in C

2 Functions - why and how ? If a problem is large If a problem is large Modularization – easier to: Modularization – easier to: codecode debugdebug Code reuse Code reuse Passing arguments to functions Passing arguments to functions –By value Returning values from functions Returning values from functions –By value

3 Functions – basic example Example 9 #include /* function prototype at start of file */ int sum(int a, int b); int main(int argc, char *argv[]){ int total = sum(4,5); /* call to the function */ printf(“The sum of 4 and 5 is %d\n”, total); } /* the function itself arguments passed by value*/ int sum(int a, int b){ return (a+b); /* return by value */ }

4 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; x y f g c d

5 Pointers made easy ? f 4300 float f; // variable that stores a float ? f_addr 4304 float *f_addr; // pointer variable that stores the address of a float f_addr = &f; // & = address operator ?4300 f f_addr NULL

Dereferencing a pointer 6 *f_addr = 3.2; // indirection operator or dereferencing f f_addr g 4308 float g=*f_addr; // indirection: g is now 3.2 f = 1.3; f f_addr

7 Pointer operations Creation Creation –int iVal, *ptr, *iPtr; –ptr = &iVal; // pointer assignment/initialization –iPtr = ptr; Pointer indirection or dereferencing Pointer indirection or dereferencing –iVal = *ptr;  *ptr is the int value pointed to by ptr

8 #include int main(int argc, char *argv[]) { int j; int *ptr; /* initialize ptr before using it */ ptr=&j; /* *ptr=4 does NOT initialize ptr */ *ptr=4; /* j = 4 */ j=*ptr+1; /* j = ? */ return 0; } Pointer Example 10

9 Pointers and arrays int p[10], *ptr; // Although p represents an array, // both p and ptr are pointers, // both p and ptr are pointers, // i.e., can hold addresses. // i.e., can hold addresses. // p is already pointing to a fixed location and // p is already pointing to a fixed location and // cannot be changed. // cannot be changed. // ptr is still to be initialized. // ptr is still to be initialized. p[i] is an int value p, &p[i] and (p+i) are addresses or pointers *p is the same as p[0] (They are both int values) *(p+i) is the same as p[i] (They are both int values)

How arrays and pointers relate int a[10]; a[0]a[1]a[9] a: int *pa; pa = &a[0]; // same as pa = a; a[0]a[1]a[9] a: pa: pa + 1: pa + 5: See below instead 10

11 Pointer arithmetic int p[10]; ptr = p; // or ptr = &p[0] ptr +=2; // ptr = ptr + 2 * sizeof(int) = ptr+8 bytes => ptr = &(p[2]); p = ptr; Gives ERROR because “p” is a constant address, points to the beginning of an array and cannot be changed.

Summary of Arrays and Pointers In C there is a strong relationship between arrays and pointers In C there is a strong relationship between arrays and pointers Any operation that can be achieved by array subscripting can be done with pointers Any operation that can be achieved by array subscripting can be done with pointers The pointer version will be faster, in general The pointer version will be faster, in general –A bit harder to understand 12

Homework 4 Another exercise on C. Another exercise on C. Available on class’s Angel page Available on class’s Angel page – follow link from the schedule page 13