63
Functions and Pointers Lecture 29

Functions and Pointers

  • Upload
    meir

  • View
    88

  • Download
    2

Embed Size (px)

DESCRIPTION

Functions and Pointers. Lecture 29. Summary of Previous Lecture. Algorithms Functions Using functions in top-down design Functions in C Language User defined Functions Parameters Return values. Topics. Functions Prototypes Variable Scope Pointers Introduction to pointers - PowerPoint PPT Presentation

Citation preview

Page 1: Functions and Pointers

Functions and Pointers

Lecture 29

Page 2: Functions and Pointers

Summary of Previous Lecture Algorithms

FunctionsUsing functions in top-down design

Functions in C LanguageUser defined FunctionsParametersReturn values

Page 3: Functions and Pointers

Topics

FunctionsPrototypesVariable Scope

Pointers Introduction to pointers Pointers and function parameters

Summary

Page 4: Functions and Pointers

Prototyping of Functions

Must declare functions before use (like variables)

Declaration is called a “prototype” Specifies the name, parameters and return

type of the function, but not the code

Page 5: Functions and Pointers

#include <stdio.h>

int isNegative (int);

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

Example: isNegative.c

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Page 6: Functions and Pointers

#include <stdio.h>

int isNegative (int);

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Example: isNegative.cFunction Prototype

Page 7: Functions and Pointers

#include <stdio.h>

int isNegative (int);

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Function Definition

Example: isNegative.c

Page 8: Functions and Pointers

#include <stdio.h>

int isNegative (int);

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Function Call (Must be after prototype, but can be before definition)

Example: isNegative.c

Page 9: Functions and Pointers

#include <stdio.h>

int isNegative (int);

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Header files (filename.h) contain function prototypes and global

variable declarations

Example: isNegative.c

Page 10: Functions and Pointers

#include <stdio.h>

int isNegative (int);

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

stdio.h contains function prototypes for printf(), scanf(), and

other I/O functions

Example: isNegative.c

Page 11: Functions and Pointers

Header files You can make your own header files with

prototypes of frequently used functions:#include "myFunctions.h"

Put the functions in a corresponding C file, and include those too:#include "myFunctions.c"

Page 12: Functions and Pointers

#include <stdio.h>#include "myFunctions.h"#include "myFunctions.c"

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

Example: isNegative.c

Note:• " " around file name for user-defined files• < > for standard system files

isNegative() is declared in myFunctions.h and defined in myFunctions.c, not in this file!

Page 13: Functions and Pointers

Example: myFunctions.c

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Example: myFunctions.h

int isNegative ( int );

Page 14: Functions and Pointers

Scope

Where can you use a variable which is declared in a function?

In that function only Not in a calling function Not in a called function

Page 15: Functions and Pointers

Scope: Local Variables Formal parameters: only accessible whilst

function executing Variables declared in a function body: only

accessible whilst function executing In fact, this is true of every block in a

program

Page 16: Functions and Pointers

#include <stdio.h>

int isNegative ( int n ){

int result;if (number<0){

result=1;}else {

result = 0;}return result;

}

Example: isNegative.c

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

Page 17: Functions and Pointers

#include <stdio.h>

int isNegative ( int n ){

int result;if (number<0){

result=1;}else {

result = 0;}return result;

}

Example: isNegative.c

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

ERROR! Number is local to the main function, not accessible

here

Page 18: Functions and Pointers

#include <stdio.h>

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Example: isNegative.c

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

Use the parameter n which is local to the function isNegative()

Page 19: Functions and Pointers

#include <stdio.h>

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Example: isNegative.c

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

number is the actual parameter

n is the formal parameter

Page 20: Functions and Pointers

#include <stdio.h>

int isNegative ( int n ){

int result;if ( n<0 ){

result=1;}else {

result = 0;}return result;

}

Example: isNegative.c

int main (void){

int number;

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative(number)){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

result & n: local to isNegative()number: local to main()

Page 21: Functions and Pointers

#include <stdio.h>

int number;

int isNegative ( void ){

int result;if ( number <0 ){

result=1;}else {

result = 0;}return result;

}

Example: isNegativeGlobal.c

int main (void){

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative()){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

Page 22: Functions and Pointers

#include <stdio.h>

int number;

int isNegative ( void ){

int result;if ( number <0 ){

result=1;}else {

result = 0;}return result;

}

Example: isNegativeGlobal.c

int main (void){

printf ("Enter an integer: ");scanf ("%d",&number);

if (isNegative()){

printf("Negative\n");}else{

printf("Positive\n");}

return 0;}

number is now GLOBAL - declared outside any function,

accessible in all functions (after the declaration)

Page 23: Functions and Pointers

Scope: Global Variables Global variables are accessible in any

function after their declaration to the end of that source file

They're useful, but risky if any and every function can modify them, it

can be difficult to keep track of their value Better to use local variables and

parameter passing if possible

Page 24: Functions and Pointers

Scope: Functions Functions are also accessible in any

function after their declaration to the end of that source file

Page 25: Functions and Pointers

Functions Summary Include function prototype before its use Be careful about the scope of variables

Page 26: Functions and Pointers

Pointers

Page 27: Functions and Pointers

Pointers Topics

Introduction to pointers Pointers and function parameters

Page 28: Functions and Pointers

What is a Pointer in C? As the name suggests, Pointer is to point

towards anything.For example pointer on the road, city etc.

Pointing towards “Road”

Page 29: Functions and Pointers

Pointers in C C pointers are used to point towards the

contents of any memory location.

Page 30: Functions and Pointers

char ch = ’A’;

’A’0x2000

ch:

Memory Address of a Variable

The value of the variable ch

The memory address of the

variable ch

Page 31: Functions and Pointers

The & Operator

• Gives the memory address of an object

• Also known as the “address operator”

&ch yields the value 0x2000

char ch = ’A’;

’A’0x2000

Page 32: Functions and Pointers

“conversion specifier” for printing a memory address

char ch;

printf(“%p”, &ch);

Example:

Page 33: Functions and Pointers

Pointers

ch

0x1FFF 0x2000 0x2001 0x20020x1FFEetc

‘B’

0x2000

chPtr

0x3A15

A variable which can store the memory address of another variable

Page 34: Functions and Pointers

Pointers

A pointer is a variable Contains a memory address Points to a specific data type Pointer variables are usually named varPtr

Page 35: Functions and Pointers

cPtr:

char* cPtr;Example:

• We say cPtr is a pointer to char

0x2004

Can store an address of variables of type char

Page 36: Functions and Pointers

Pointers and the & Operator

Example:

A

c:

0x2000

char c = ’A’;

char *cPtr;

cPtr:

0x2004

cPtr = &c;

0x2000

Assigns the address of c to cPtr

Page 37: Functions and Pointers

Notes on Pointers

int* numPtr;float* xPtr;

Example:

• We can have pointers to any data type

int *numPtr;float * xPtr;

Example:

• The * can be anywhere between the type and the variable

Page 38: Functions and Pointers

Notes on Pointers ..

• You can print the address stored in a pointer using the %p conversion specifier

printf(“%p”, numPtr);Example:

• You can assign the address of a variable to a “compatible” pointer using the & operator

int aNumber;int *numPtr;

numPtr = &aNumber;

Example:

Page 39: Functions and Pointers

The * Operator

• Allows pointers to access variables they point to

• Also known as “dereferencing operator”

• Should not be confused with the * in the pointer declaration

Page 40: Functions and Pointers

A

c:

0x2000

B

Pointers and the Operator

Example: char c = ’A’;

char *cPtr = NULL;

cPtr = &c;

*cPtr = ’B’;

Changes the value of the variable which cPtr

points to

cPtr:

0x2004

NULL0x2000

Page 41: Functions and Pointers

Easy Steps to Pointers Step 1: Declare the variable to be pointed to

int num;char ch = ‘A’;float x;

num:

‘A’ch:

x:

Page 42: Functions and Pointers

Easy Steps to Pointers … Step 2: Declare the pointer variable

int* numPtr = NULL;char *chPtr = NULL;float * xPtr = NULL;

int num;char ch = ‘A’;float x;

numPtr:

chPtr:

xPtr:

num:

‘A’ch:

x:

NULL

NULL

NULL

Page 43: Functions and Pointers

Easy Steps to Pointers .. Step 3: Assign address of variable to pointer

int* numPtr = NULL;char *chPtr = NULL;float * xPtr = NULL;

int num;char ch = ‘A’;float x;

numPtr = &num;

numPtr: addr of num

addr of chchPtr:

addr of xxPtr:

num:

‘A’ch:

x:A pointer’s type has to correspond to the type of the variable it points to

chPtr = &ch;xPtr = &x;

Page 44: Functions and Pointers

Easy Steps to Pointers .. Step 4: De-reference the pointers

int* numPtr = NULL;char *chPtr = NULL;float * xPtr = NULL;

int num;char ch = ‘A’;float x;

numPtr = &num;chPtr = &ch;xPtr = &x;

*xPtr = 0.25;*numPtr = *chPtr;

num: 65

‘A’ch:

0.25x:

numPtr: addr of num

addr of chchPtr:

addr of xxPtr:

Page 45: Functions and Pointers

Notes on Pointers ..

int *numPtr;Beware of pointers

which are not initialized!

???

numPtr

Page 46: Functions and Pointers

Notes on Pointers ..

int *numPtr = NULL;

NULL

numPtr

• When declaring a pointer, it is a good idea to always initialize it to NULL (a special pointer constant)

Page 47: Functions and Pointers

Pointers and Function Parameters

Example: Function to swap the values of two variables

x: 1

y: 2swap

x: 2

y: 1

Page 48: Functions and Pointers

#include <stdio.h>

void swap1(int a, int b){ int tmp;

tmp = a; a = b; b = tmp; return;}

int main(){ int x = 1, y = 2;

swap1(x, y); printf(“%d %d\n”, x, y); return 0;}

Bad swap

Page 49: Functions and Pointers

#include <stdio.h>

void swap1(int a, int b){ int tmp;

tmp = a; a = b; b = tmp; return;}

int main(){ int x = 1, y = 2;

swap1(x, y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

Bad swap

Page 50: Functions and Pointers

#include <stdio.h>

void swap1(int a, int b){ int tmp;

tmp = a; a = b; b = tmp; return;}

int main(){ int x = 1, y = 2;

swap1(x, y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

1

2

a:

b:

tmp:

Bad swap

Page 51: Functions and Pointers

#include <stdio.h>

void swap1(int a, int b){ int tmp;

tmp = a; a = b; b = tmp; return;}

int main(){ int x = 1, y = 2;

swap1(x, y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

1

2

a:

b:

1tmp:

Bad swap

Page 52: Functions and Pointers

#include <stdio.h>

void swap1(int a, int b){ int tmp;

tmp = a; a = b; b = tmp; return;}

int main(){ int x = 1, y = 2;

swap1(x, y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

2

2

a:

b:

1tmp:

Bad swap

Page 53: Functions and Pointers

#include <stdio.h>

void swap1(int a, int b){ int tmp;

tmp = a; a = b; b = tmp; return;}

int main(){ int x = 1, y = 2;

swap1(x, y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

2

1

a:

b:

1tmp:

Bad swap

Page 54: Functions and Pointers

#include <stdio.h>

void swap1(int a, int b){ int tmp;

tmp = a; a = b; b = tmp; return;}

int main(){ int x = 1, y = 2;

swap1(x, y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

2

1

a:

b:

1tmp:

Bad swap

Page 55: Functions and Pointers

#include <stdio.h>

void swap2(int* a, int* b){ int tmp;

tmp = *a; *a = *b; *b = tmp; return;}

int main(){ int x = 1, y = 2;

swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}

Good swap

Page 56: Functions and Pointers

#include <stdio.h>

void swap2(int* a, int* b){ int tmp;

tmp = *a; *a = *b; *b = tmp; return;}

int main(){ int x = 1, y = 2;

swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

Good swap

Page 57: Functions and Pointers

#include <stdio.h>

void swap2(int* a, int* b){ int tmp;

tmp = *a; *a = *b; *b = tmp; return;}

int main(){ int x = 1, y = 2;

swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

addr of x

addr of y

a:

b:

tmp:

Good swap

Page 58: Functions and Pointers

#include <stdio.h>

void swap2(int* a, int* b){ int tmp;

tmp = *a; *a = *b; *b = tmp; return;}

int main(){ int x = 1, y = 2;

swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}

1

2

x:

y:

addr of x

addr of y

a:

b:

1tmp:

Good swap

Page 59: Functions and Pointers

#include <stdio.h>

void swap2(int* a, int* b){ int tmp;

tmp = *a; *a = *b; *b = tmp; return;}

int main(){ int x = 1, y = 2;

swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}

2

2

x:

y:

addr of x

addr of y

a:

b:

1tmp:

Good swap

Page 60: Functions and Pointers

#include <stdio.h>

void swap2(int* a, int* b){ int tmp;

tmp = *a; *a = *b; *b = tmp; return;}

int main(){ int x = 1, y = 2;

swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}

2

1

x:

y:

addr of x

addr of y

a:

b:

1tmp:

Good swap

Page 61: Functions and Pointers

#include <stdio.h>

void swap2(int* a, int* b){ int tmp;

tmp = *a; *a = *b; *b = tmp; return;}

int main(){ int x = 1, y = 2;

swap2(&x, &y); printf(“%d %d\n”, x, y); return 0;}

2

1

x:

y:

Good swap

Page 62: Functions and Pointers

Pointers and Function Arguments

Change the value of an actual parameter variable

scanf exposed!

char ch;

int numx;

float numy;

scanf(“%c %d %f”, &ch, &numx, &numy);

Page 63: Functions and Pointers

Summary of Pointers Pointers point towards the content of

another memory location.Address of the other memory location is

stored in the pointer variable towards which it is pointing.

Pointers needed to be declared before used.

Never left any pointer un-initialized.