University of Malta CSA2090: Lecture 4 © 2004- Chris Staff 1 of 20 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Introduction to Programming Lecture 39. Copy Constructor.
Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
Kernighan/Ritchie: Kelley/Pohl:
Lecture 2 Introduction to C Programming
CSI 101 Elements of Computing Spring 2009 Lecture #10 – Functions and Subroutines Monday, March 16th.
1 CSE1301 Computer Programming Lecture 16 Pointers.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
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.
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);
1 CSE1301 Computer Programming Lecture 16 Pointers.
Computer Science 210 Computer Organization Pointers.
University of Malta CSA3080: Lecture 9 © Chris Staff 1 of 13 CSA3080: Adaptive Hypertext Systems I Dr. Christopher Staff Department.
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
1 Lecture04: Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Computer Science 210 Computer Organization Arrays.
University of Malta CSA3080: Lecture 4 © Chris Staff 1 of 14 CSA3080: Adaptive Hypertext Systems I Dr. Christopher Staff Department.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Workin’ with Pointas An exercise in destroying your computer.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Computer science C programming language lesson 3.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
University of Malta CSA2090: Lecture 5 © Chris Staff 1 of 22 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C Programming Lecture 8 Call by Reference Scope. Call-by-Reference b For a C function to “effect” (read as “imitate”) call-by- reference: Pointers must.
Pointers and Classes.
Computer Science 210 Computer Organization
Introduction to Programming Using C
Chapter 10: Void Functions
FIGURE 4-10 Function Return Statements
CISC181 Introduction to Computer Science Dr
Tejalal Choudhary “C Programming from Scratch” Pointers
Computer Science 210 Computer Organization
CSC 253 Lecture 8.
CSC 253 Lecture 8.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Lecture 18 Arrays and Pointer Arithmetic
Pointers Call-by-Reference CSCI 230
Functions Pass By Value Pass by Reference
Topics discussed in this section:
Dynamic Memory A whole heap of fun….
Module 2-3: Passing pointers
CISC181 Introduction to Computer Science Dr
Strings and Pointer Arrays
C++ Pointers and Strings
C Programming Pointers
The Stack.
Linked List Functions.
C++ Pointers and Strings
CS148 Introduction to Programming II
Introduction to Pointers
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

University of Malta CSA2090: Lecture 4 © Chris Staff 1 of 20 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff Department of Computer Science & AI University of Malta Lecture 4: Functions & Pointers

University of Malta CSA2090: Lecture 4 © Chris Staff 2 of 20 Aims and Objectives The definition of function allows a C program to be modularised C permits memory locations to be accessed directly as well as through variable names

University of Malta CSA2090: Lecture 4 © Chris Staff 3 of 20 Functions C has no procedures or methods, only functions Functions can take 0, 1, or more parameters or arguments Functions return exactly one value

University of Malta CSA2090: Lecture 4 © Chris Staff 4 of 20 Form of a function ( ) { }

University of Malta CSA2090: Lecture 4 © Chris Staff 5 of 20 Example function int mean(int a, int b) { int tmp; tmp = (a + b) / 2; return tmp; }

University of Malta CSA2090: Lecture 4 © Chris Staff 6 of 20 Function behaviour Functions end when –a closing } is reached –a return statement is reached returns control to the calling function –an exit statement is reached terminates the program –a crash occurs

University of Malta CSA2090: Lecture 4 © Chris Staff 7 of 20 C function arguments All parameters are ‘passed by value’ int main() { int a = 1277, x; … x = foo(a); }

University of Malta CSA2090: Lecture 4 © Chris Staff 8 of 20 C function arguments When foo is entered a copy of a is created int main() { int a = 1277, x; … x = foo(a); } int foo(int a) {… }

University of Malta CSA2090: Lecture 4 © Chris Staff 9 of 20 C function arguments To change the value of a in main() int main() { int a = 1277; … a = foo(a); } int foo(int a) {a = a*2; return a;}

University of Malta CSA2090: Lecture 4 © Chris Staff 10 of 20 C function arguments To change the value of a in main() int main() { int a = 1277; … a = foo(a); } int foo(int a) {a = a*2; return a;}

University of Malta CSA2090: Lecture 4 © Chris Staff 11 of 20 C function arguments To change the value of a in main() int main() { int a = 1277; … a = foo(a); } int foo(int a) {a = a*2; return a;} 1 2

University of Malta CSA2090: Lecture 4 © Chris Staff 12 of 20 Call by reference Being able to return one value means that you can only change the value of one variable To change the values of other variables, we must tell the called function where they exist in memory

University of Malta CSA2090: Lecture 4 © Chris Staff 13 of 20 Pointers C supports reference to a memory address using the & operator &a will return the address of variable a in memory &a is 100 Addresses can be printed using %u

University of Malta CSA2090: Lecture 4 © Chris Staff 14 of 20 Pointers To retrieve or access the value stored at a memory address, use the * operator a is 1277 You can use & with any variable, but * dereferences the value

University of Malta CSA2090: Lecture 4 © Chris Staff 15 of 20 Declaring pointers int *p; // p is pointer to int p = &a; // p contains address of a printf(“%d\n”, *p);

University of Malta CSA2090: Lecture 4 © Chris Staff 16 of 20 Declaring pointers int *p; // p is pointer to int p = &a; // p contains address of a printf(“%d\n”, *p); &p = ? *a = ?

University of Malta CSA2090: Lecture 4 © Chris Staff 17 of 20 Declaring pointers int *p; // p is pointer to int p = &a; // p contains address of a printf(“%d\n”, *p); &p = 200 *a = garbage!! a = 10 *p = ?

University of Malta CSA2090: Lecture 4 © Chris Staff 18 of 20 Declaring pointers int *p; // p is pointer to int p = &a; // p contains address of a printf(“%d\n”, *p); &p = 200 *a = garbage!! p = 10 *p = ?

University of Malta CSA2090: Lecture 4 © Chris Staff 19 of 20 Nice try! int main() { int *p, bar=10; p = foo(bar); } int *foo(int bar){ int x = bar * 2; return &x; }

University of Malta CSA2090: Lecture 4 © Chris Staff 20 of 20 Next lecture Pointers, structures and strings