1 / 63

Functions and Pointers

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

meir
Download Presentation

Functions and Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions and Pointers Lecture 29

  2. Summary of Previous Lecture • Algorithms • Functions • Using functions in top-down design • Functions in C Language • User defined Functions • Parameters • Return values

  3. Topics • Functions • Prototypes • Variable Scope • Pointers • Introduction to pointers • Pointers and function parameters • Summary

  4. 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

  5. Example: isNegative.c #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; }

  6. Function Prototype Example: isNegative.c #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; }

  7. Example: isNegative.c #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

  8. Example: isNegative.c #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)

  9. Example: isNegative.c #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

  10. Example: isNegative.c #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

  11. 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"

  12. Example: isNegative.c #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; } • 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!

  13. Example: myFunctions.h Example: myFunctions.c int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } int isNegative ( int );

  14. 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

  15. 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

  16. Example: isNegative.c #include <stdio.h> int isNegative ( int n ) { int result; if (number<0) { result=1; } else { result = 0; } return result; } int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; }

  17. Example: isNegative.c #include <stdio.h> int isNegative ( int n ) { int result; if (number<0) { result=1; } else { result = 0; } return result; } 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

  18. Example: isNegative.c #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } 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()

  19. Example: isNegative.c #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } n is the formal parameter number is the actual parameter

  20. Example: isNegative.c #include <stdio.h> int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } 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()

  21. Example: isNegativeGlobal.c #include <stdio.h> int number; int isNegative ( void ) { int result; if ( number <0 ) { result=1; } else { result = 0; } return result; } int main (void) { printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative()) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; }

  22. Example: isNegativeGlobal.c #include <stdio.h> int number; int isNegative ( void ) { int result; if ( number <0 ) { result=1; } else { result = 0; } return result; } 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)

  23. 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

  24. Scope: Functions • Functions are also accessible in any function after their declaration to the end of that source file

  25. Functions Summary • Include function prototypebefore its use • Be careful about the scope of variables

  26. Pointers

  27. Pointers Topics • Introduction to pointers • Pointers and function parameters

  28. 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”

  29. Pointers in C • C pointers are used to point towards the contents of any memory location.

  30. ch: ’A’ 0x2000 Memory Address of a Variable char ch = ’A’; The memory address of the variable ch The value of the variable ch

  31. char ch = ’A’; ’A’ 0x2000 &ch yields the value 0x2000 The &Operator • Gives the memory address of an object • Also known as the “address operator”

  32. “conversion specifier” for printing a memory address Example: char ch; printf(“%p”, &ch);

  33. 0x3A15 0x2000 chPtr Pointers A variable which can store the memory address of another variable 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc ‘B’ ch

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

  35. Example: char* cPtr; cPtr: 0x2004 Can store an address of variables of type char • We say cPtris a pointer to char

  36. c: cPtr: A 0x2000 0x2004 Pointers and the & Operator Example: char c = ’A’; char *cPtr; cPtr = &c; Assigns the address ofc to cPtr 0x2000

  37. We can have pointers to any data type Example: int* numPtr; float* xPtr; • The * can be anywhere between the type and the variable Example: int *numPtr; float * xPtr; Notes on Pointers

  38. You can assign the address of a variable to a “compatible” pointer using the &operator int aNumber; int *numPtr; numPtr = &aNumber; Example: • You can print the address stored in a pointer using the %pconversion specifier Example: printf(“%p”, numPtr); Notes on Pointers ..

  39. 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

  40. 0x2000 cPtr: c: A NULL 0x2000 0x2004 Pointers and the  Operator Example: char c = ’A’; char *cPtr = NULL; Changes the value of the variable which cPtr points to cPtr = &c; *cPtr = ’B’; B

  41. num: ch: ‘A’ x: Easy Steps to Pointers • Step 1: Declare the variable to be pointed to int num; char ch = ‘A’; float x;

  42. NULL NULL NULL Easy Steps to Pointers … • Step 2: Declare the pointer variable int num; char ch = ‘A’; float x; numPtr: chPtr: int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: num: ch: ‘A’ x:

  43. Easy Steps to Pointers .. • Step 3: Assign address of variable to pointer int num; char ch = ‘A’; float x; numPtr: addr of num chPtr: addr of ch int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: addr of x numPtr = &num; num: chPtr = &ch; xPtr = &x; ch: ‘A’ x: A pointer’s type has to correspond to the type of the variable it points to

  44. Easy Steps to Pointers .. • Step 4: De-reference the pointers int num; char ch = ‘A’; float x; numPtr: addr of num chPtr: addr of ch int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: addr of x numPtr = &num; chPtr = &ch; xPtr = &x; num: 65 ch: ‘A’ *xPtr = 0.25; *numPtr = *chPtr; x: 0.25

  45. Notes on Pointers .. Beware of pointers which are not initialized! int *numPtr; ??? numPtr

  46. Notes on Pointers .. • When declaring a pointer, it is a good idea to always initialize it toNULL (a special pointer constant) int *numPtr = NULL; NULL numPtr

  47. Pointers and Function Parameters • Example: Function to swap the values of two variables swap x: 1 y: 2 x: 2 y: 1

  48. Bad swap #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; }

  49. Bad swap #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; } x: 1 y: 2

  50. Bad swap #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; } tmp: a: 1 b: 2 x: 1 y: 2

More Related