🗊 Презентация Pointers. Lecture18-20

Нажмите для полного просмотра!
Pointers. Lecture18-20, слайд №1 Pointers. Lecture18-20, слайд №2 Pointers. Lecture18-20, слайд №3 Pointers. Lecture18-20, слайд №4 Pointers. Lecture18-20, слайд №5 Pointers. Lecture18-20, слайд №6 Pointers. Lecture18-20, слайд №7 Pointers. Lecture18-20, слайд №8 Pointers. Lecture18-20, слайд №9 Pointers. Lecture18-20, слайд №10 Pointers. Lecture18-20, слайд №11 Pointers. Lecture18-20, слайд №12 Pointers. Lecture18-20, слайд №13 Pointers. Lecture18-20, слайд №14 Pointers. Lecture18-20, слайд №15 Pointers. Lecture18-20, слайд №16 Pointers. Lecture18-20, слайд №17 Pointers. Lecture18-20, слайд №18 Pointers. Lecture18-20, слайд №19 Pointers. Lecture18-20, слайд №20 Pointers. Lecture18-20, слайд №21 Pointers. Lecture18-20, слайд №22 Pointers. Lecture18-20, слайд №23 Pointers. Lecture18-20, слайд №24 Pointers. Lecture18-20, слайд №25 Pointers. Lecture18-20, слайд №26 Pointers. Lecture18-20, слайд №27 Pointers. Lecture18-20, слайд №28 Pointers. Lecture18-20, слайд №29 Pointers. Lecture18-20, слайд №30 Pointers. Lecture18-20, слайд №31 Pointers. Lecture18-20, слайд №32 Pointers. Lecture18-20, слайд №33 Pointers. Lecture18-20, слайд №34 Pointers. Lecture18-20, слайд №35 Pointers. Lecture18-20, слайд №36 Pointers. Lecture18-20, слайд №37 Pointers. Lecture18-20, слайд №38 Pointers. Lecture18-20, слайд №39 Pointers. Lecture18-20, слайд №40 Pointers. Lecture18-20, слайд №41 Pointers. Lecture18-20, слайд №42 Pointers. Lecture18-20, слайд №43 Pointers. Lecture18-20, слайд №44 Pointers. Lecture18-20, слайд №45 Pointers. Lecture18-20, слайд №46 Pointers. Lecture18-20, слайд №47 Pointers. Lecture18-20, слайд №48 Pointers. Lecture18-20, слайд №49 Pointers. Lecture18-20, слайд №50 Pointers. Lecture18-20, слайд №51 Pointers. Lecture18-20, слайд №52 Pointers. Lecture18-20, слайд №53 Pointers. Lecture18-20, слайд №54 Pointers. Lecture18-20, слайд №55 Pointers. Lecture18-20, слайд №56 Pointers. Lecture18-20, слайд №57 Pointers. Lecture18-20, слайд №58 Pointers. Lecture18-20, слайд №59 Pointers. Lecture18-20, слайд №60 Pointers. Lecture18-20, слайд №61 Pointers. Lecture18-20, слайд №62 Pointers. Lecture18-20, слайд №63 Pointers. Lecture18-20, слайд №64 Pointers. Lecture18-20, слайд №65 Pointers. Lecture18-20, слайд №66 Pointers. Lecture18-20, слайд №67 Pointers. Lecture18-20, слайд №68 Pointers. Lecture18-20, слайд №69 Pointers. Lecture18-20, слайд №70 Pointers. Lecture18-20, слайд №71 Pointers. Lecture18-20, слайд №72 Pointers. Lecture18-20, слайд №73 Pointers. Lecture18-20, слайд №74 Pointers. Lecture18-20, слайд №75 Pointers. Lecture18-20, слайд №76 Pointers. Lecture18-20, слайд №77 Pointers. Lecture18-20, слайд №78 Pointers. Lecture18-20, слайд №79 Pointers. Lecture18-20, слайд №80 Pointers. Lecture18-20, слайд №81 Pointers. Lecture18-20, слайд №82 Pointers. Lecture18-20, слайд №83 Pointers. Lecture18-20, слайд №84 Pointers. Lecture18-20, слайд №85 Pointers. Lecture18-20, слайд №86 Pointers. Lecture18-20, слайд №87 Pointers. Lecture18-20, слайд №88 Pointers. Lecture18-20, слайд №89 Pointers. Lecture18-20, слайд №90 Pointers. Lecture18-20, слайд №91 Pointers. Lecture18-20, слайд №92 Pointers. Lecture18-20, слайд №93 Pointers. Lecture18-20, слайд №94 Pointers. Lecture18-20, слайд №95 Pointers. Lecture18-20, слайд №96 Pointers. Lecture18-20, слайд №97 Pointers. Lecture18-20, слайд №98 Pointers. Lecture18-20, слайд №99 Pointers. Lecture18-20, слайд №100 Pointers. Lecture18-20, слайд №101 Pointers. Lecture18-20, слайд №102

Содержание

Вы можете ознакомиться и скачать презентацию на тему Pointers. Lecture18-20. Доклад-сообщение содержит 102 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1


Lecture 18-20 Pointers
Описание слайда:
Lecture 18-20 Pointers

Слайд 2


Outline Defining and using Pointers Operations on pointers Arithmetic Logical Pointers and Arrays Memory Management for Pointers
Описание слайда:
Outline Defining and using Pointers Operations on pointers Arithmetic Logical Pointers and Arrays Memory Management for Pointers

Слайд 3


Pointer Fundamentals When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable int x; When a...
Описание слайда:
Pointer Fundamentals When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable int x; When a value is assigned to a variable, the value is actually placed to the memory that was allocated x=3;

Слайд 4


Recall Variables
Описание слайда:
Recall Variables

Слайд 5


Recall Variables Recall a variable is nothing more than a convenient name for a memory location. The type of the variable (e.g., int) defines how the...
Описание слайда:
Recall Variables Recall a variable is nothing more than a convenient name for a memory location. The type of the variable (e.g., int) defines how the bits inside that memory location will be interpreted, and what operations are permitted on this variable. Every variable has an address. Every variable has a value.

Слайд 6


The Real Variable Name is its Address! There are 4 billion (232) different addresses, and hence 4 billion different memory locations. Each memory...
Описание слайда:
The Real Variable Name is its Address! There are 4 billion (232) different addresses, and hence 4 billion different memory locations. Each memory location is a variable (whether your program uses it or not). Your program will probably only create names for a small subset of these “potential variables”. Some variables are guarded by the operating system and cannot be accessed. When your program uses a variable the compiler inserts machine code that calculates the address of the variable. Only by knowing the address can the variables be accessed.

Слайд 7


Pointers When the value of a variable is used, the contents in the memory are used y=x; will read the contents in the 4 bytes of memory, and then...
Описание слайда:
Pointers When the value of a variable is used, the contents in the memory are used y=x; will read the contents in the 4 bytes of memory, and then assign it to variable y &x can get the address of x (referencing operator &) The address can be passed to a function: scanf("%d", &x); The address can also be stored in a variable ……

Слайд 8


Pointer: Reference to Memory
Описание слайда:
Pointer: Reference to Memory

Слайд 9


Pointers To declare a pointer variable type * PointerName; For example: int x; int * p; //p is a int pointer // char *p2; p = &x; /* Initializing p */
Описание слайда:
Pointers To declare a pointer variable type * PointerName; For example: int x; int * p; //p is a int pointer // char *p2; p = &x; /* Initializing p */

Слайд 10


Pointer: Declaration and Initialization
Описание слайда:
Pointer: Declaration and Initialization

Слайд 11


Addresses and Pointers int a, b; int *c, *d; a = 5; c = &a; d = &b; *d = 9; printf(…,c, *c,&c) printf(…,a, b)
Описание слайда:
Addresses and Pointers int a, b; int *c, *d; a = 5; c = &a; d = &b; *d = 9; printf(…,c, *c,&c) printf(…,a, b)

Слайд 12


Addresses and Pointers A pointer variable is a variable! It is stored in memory somewhere and has an address. It is a string of bits (just like any...
Описание слайда:
Addresses and Pointers A pointer variable is a variable! It is stored in memory somewhere and has an address. It is a string of bits (just like any other variable). Pointers are 32 bits long on most systems.

Слайд 13


Using Pointers You can use pointers to access the values of other variables, i.e. the contents of the memory for other variables To do this, use the...
Описание слайда:
Using Pointers You can use pointers to access the values of other variables, i.e. the contents of the memory for other variables To do this, use the * operator (dereferencing operator) Depending on different context, * has different meanings

Слайд 14


* has different meanings in different contexts a = x * y;  multiplication int *ptr;  declare a pointer * is also used as indirection or...
Описание слайда:
* has different meanings in different contexts a = x * y;  multiplication int *ptr;  declare a pointer * is also used as indirection or de-referencing operator in C statements. ptr = &y; a = x * *ptr;

Слайд 15


Using Pointers You can use pointers to access the values of other variables, i.e. the contents of the memory for other variables To do this, use the...
Описание слайда:
Using Pointers You can use pointers to access the values of other variables, i.e. the contents of the memory for other variables To do this, use the * operator (dereferencing operator) Depending on different context, * has different meanings For example: int n, m = 3, *p; p = &m; // Initializing n = *p; printf("%d\n", n); // 3 printf("%d\n", *p); // 3 *p = 10; printf("%d\n", n); // 3 printf("%d\n", *p); // 10

Слайд 16


An Example int m = 3, n = 100, *p, *q; p = &m; printf("m is %d\n", *p); // 3 m++; printf("now m is %d\n", *p); // 4 p = &n;...
Описание слайда:
An Example int m = 3, n = 100, *p, *q; p = &m; printf("m is %d\n", *p); // 3 m++; printf("now m is %d\n", *p); // 4 p = &n; printf("n is %d\n", *p); // 100 *p = 500; printf("now n is %d\n", n); // 500 q = &m; *q = *p; printf("now m is %d\n", m); // 500

Слайд 17


An Example int i = 25; int *p; p = &i; printf("%x %x", &p, &i); printf("%x %p", p, p); printf("%d %d", i, *p);
Описание слайда:
An Example int i = 25; int *p; p = &i; printf("%x %x", &p, &i); printf("%x %p", p, p); printf("%d %d", i, *p);

Слайд 18


Pointer Assignment int a = 2, b = 3; int *p1, *p2; p1 = &a; p2 = &b; printf("%p %p", p1 ,p2); *p1 = *p2; printf("%d %d", *p1,...
Описание слайда:
Pointer Assignment int a = 2, b = 3; int *p1, *p2; p1 = &a; p2 = &b; printf("%p %p", p1 ,p2); *p1 = *p2; printf("%d %d", *p1, *p2); p2 = p1; printf("%p %p", p1, p2); printf("%p %p", &p1, &p2);

Слайд 19


Value of referred memory by a pointer
Описание слайда:
Value of referred memory by a pointer

Слайд 20


Exercise: Trace the following code int x, y; int *p1, *p2; x = 3 + 4; Y = x / 2 + 5; p1 = &y; p2 = &x; *p1 = x + *p2; *p2 = *p1 + y;...
Описание слайда:
Exercise: Trace the following code int x, y; int *p1, *p2; x = 3 + 4; Y = x / 2 + 5; p1 = &y; p2 = &x; *p1 = x + *p2; *p2 = *p1 + y; printf(…,p1,*p1,&p1) printf(…,x,&x,y,&y)

Слайд 21


Pointer Fundamentals When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable int x; // &x =...
Описание слайда:
Pointer Fundamentals When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable int x; // &x = 22f54; &x = 22f54; // Error When a value is assigned to a variable, the value is actually placed to the memory that was allocated x = 3; // * (&x) = 3; *x = 3; // Error

Слайд 22


Allocating Memory for a Pointer
Описание слайда:
Allocating Memory for a Pointer

Слайд 23


Characteristics of Pointers We’ve seen that pointers can be initialized and assigned (like any variable can). They can be local or global variables...
Описание слайда:
Characteristics of Pointers We’ve seen that pointers can be initialized and assigned (like any variable can). They can be local or global variables (or parameters) You can have an array of pointers etc., just like any other kind of variable. We’ve also seen the dereference operator (*). This is the operation that really makes pointers special (pointers are the only type of variable that can be dereferenced).

Слайд 24


Pointer “Size” Note: Pointers are all the same size. On most computers, a pointer variable is four bytes (32 bits). However, the variable that a...
Описание слайда:
Pointer “Size” Note: Pointers are all the same size. On most computers, a pointer variable is four bytes (32 bits). However, the variable that a pointer points to can be arbitrary sizes. A char* pointer points at variables that are one byte long. A double* pointer points at variables that are eight bytes long. When pointer arithmetic is performed, the actual address stored in the pointer is computed based on the size of the variables being pointed at.

Слайд 25


Constant Pointers A pointer to const data does not allow modification of the data through the pointer const int a = 10, b = 20; a = 5; // Error const...
Описание слайда:
Constant Pointers A pointer to const data does not allow modification of the data through the pointer const int a = 10, b = 20; a = 5; // Error const int *p; int *q; p = &a; // or p=&b; *p = 100; // Error : p is (const int *) p = &b; q = &a; *q = 100; // OK !!!

Слайд 26


Constant Pointers int x; /* define x */ int y; /* define y */ /*ptr is a constant pointer to an integer that can be modified through ptr, but ptr...
Описание слайда:
Constant Pointers int x; /* define x */ int y; /* define y */ /*ptr is a constant pointer to an integer that can be modified through ptr, but ptr always points to the same memory location */ int * const ptr = &x; *ptr = 7; /* allowed: *ptr is not const */ ptr = &y; /* error: cannot assign new address */

Слайд 27


Constant Pointers int x = 5; /* initialize x */ int y; /* define y */ /*ptr is a constant pointer to a constant integer. ptr always points to the...
Описание слайда:
Constant Pointers int x = 5; /* initialize x */ int y; /* define y */ /*ptr is a constant pointer to a constant integer. ptr always points to the same location; the integer at that location cannot be modified */ const int * const ptr = &x; *ptr = 7; /* error: cannot assign new value */ ptr = &y; /* error: cannot assign new address */

Слайд 28


Pointer to pointer int main(void) { int s = 1; int t = 1; int *ps = &s; int **pps = &ps; int *pt = &t; **pps = 2; pt = ps; *pt = 3; return 0; }
Описание слайда:
Pointer to pointer int main(void) { int s = 1; int t = 1; int *ps = &s; int **pps = &ps; int *pt = &t; **pps = 2; pt = ps; *pt = 3; return 0; }

Слайд 29


Multiple indirection int a = 3; int *b = &a; int **c = &b; int ***d = &c; int ****f = &d;
Описание слайда:
Multiple indirection int a = 3; int *b = &a; int **c = &b; int ***d = &c; int ****f = &d;

Слайд 30


Pointer Initialization
Описание слайда:
Pointer Initialization

Слайд 31


NULL Pointer Special constant pointer NULL Points to no data Dereferencing illegal To define, include int *q = NULL;
Описание слайда:
NULL Pointer Special constant pointer NULL Points to no data Dereferencing illegal To define, include int *q = NULL;

Слайд 32


NULL Pointer
Описание слайда:
NULL Pointer

Слайд 33


NULL Pointer Often used as the return type of functions that return a pointer to indicate function failure int *myPtr; myPtr = myFunction( ); if...
Описание слайда:
NULL Pointer Often used as the return type of functions that return a pointer to indicate function failure int *myPtr; myPtr = myFunction( ); if (myPtr == NULL){ /* something bad happened */ } Dereferencing a pointer whose value is NULL will result in program termination.

Слайд 34


Generic Pointers: void * void *: a pointer to anything Lose all information about what type of thing is pointed to Reduces effectiveness of...
Описание слайда:
Generic Pointers: void * void *: a pointer to anything Lose all information about what type of thing is pointed to Reduces effectiveness of compiler’s type-checking Can’t use pointer arithmetic

Слайд 35


Operations on Pointers
Описание слайда:
Operations on Pointers

Слайд 36


Arithmetic Operations When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of...
Описание слайда:
Arithmetic Operations When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to For example, if the pointer p contains the address of a double precision variable and that address is 234567870, then the statement: p = p + 2; // 234567870 + 2 * sizeof(double) would change p to 234567886

Слайд 37


Operations on Pointers
Описание слайда:
Operations on Pointers

Слайд 38


Arithmetic Operations A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be...
Описание слайда:
Arithmetic Operations A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another int a, b; int *p = &a, *q = &b; p = p + q ; // Error p = p * q; // Error p = p / q; // Error p = p - q; // OK p = p + 3; p += 1.6; // Error p %= q; // Error

Слайд 39


Arithmetic Operations pointer + number pointer – number
Описание слайда:
Arithmetic Operations pointer + number pointer – number

Слайд 40


Comparing Pointers Pointers can also be compared using ==, !=, , = Two pointers are “equal” if they point to the same variable (i.e., the pointers...
Описание слайда:
Comparing Pointers Pointers can also be compared using ==, !=, , = Two pointers are “equal” if they point to the same variable (i.e., the pointers have the same value!) A pointer p is “less than” some other pointer q if the address currently stored in p is smaller than the address currently stored in q. It is rarely useful to compare pointers with

Слайд 41


Logical Operations Pointers can be used in comparisons int a[10], *p, *q , i; p = &a[2]; q = &a[5]; i = q - p; /* i is 3*/ i = p - q; /* i is -3 */...
Описание слайда:
Logical Operations Pointers can be used in comparisons int a[10], *p, *q , i; p = &a[2]; q = &a[5]; i = q - p; /* i is 3*/ i = p - q; /* i is -3 */ a[2] = a[5] = 0; i = *p - *q; // i = a[2] – a[5] if (p < q) ...; /* true */ if (p == q)...; /* false */ if (p != q) ...; /* true */

Слайд 42


Pointers and Arrays the value of an array name is also an address In fact, pointers and array names can be used interchangeably in many (but not all)...
Описание слайда:
Pointers and Arrays the value of an array name is also an address In fact, pointers and array names can be used interchangeably in many (but not all) cases The major differences are: Array names come with valid spaces where they "point" to. And you cannot "point" the names to other places. Pointers do not point to valid space when they are created. You have to point them to some valid space (initialization)

Слайд 43


Pointers and Arrays
Описание слайда:
Pointers and Arrays

Слайд 44


Pointers and Arrays
Описание слайда:
Pointers and Arrays

Слайд 45


An Array Name is Like a Constant Pointer Array name is like a constant pointer which points to the first element of the array int a[10], *p, *q; p =...
Описание слайда:
An Array Name is Like a Constant Pointer Array name is like a constant pointer which points to the first element of the array int a[10], *p, *q; p = a; /* p = &a[0] */ q = a + 3; /* q = &a[0] + 3 */ a ++; /* Error !!! */

Слайд 46


Example int a[10], i; int *p = a; // int *p = &a[0]; for (i = 0; i < 10; i++) scanf("%d", a + i); // scanf("%d", &a[i]); for (i =...
Описание слайда:
Example int a[10], i; int *p = a; // int *p = &a[0]; for (i = 0; i < 10; i++) scanf("%d", a + i); // scanf("%d", &a[i]); for (i = 9; i >= 0; --i) printf("%d", *(p + i)); // printf("%d", a[i]); //printf("%d", p[i]); for (p = a; p < &a[10]; p++) printf("%d", *p);

Слайд 47


An example int a[10], *p, *q; p = &a[2]; q = p + 3; p = q – 1; p++; q--; *p = 123; *q = *p; q = p; printf("%d", *q); /*...
Описание слайда:
An example int a[10], *p, *q; p = &a[2]; q = p + 3; p = q – 1; p++; q--; *p = 123; *q = *p; q = p; printf("%d", *q); /* printf("%d", a[5]) */

Слайд 48


An Example int a[10], *p; a++; //Error a--; // Error a += 3; //Error p = a; // p = &a[0]; p ++; //OK p--; // Ok P +=3; // Ok
Описание слайда:
An Example int a[10], *p; a++; //Error a--; // Error a += 3; //Error p = a; // p = &a[0]; p ++; //OK p--; // Ok P +=3; // Ok

Слайд 49


Array Example Using a Pointer
Описание слайда:
Array Example Using a Pointer

Слайд 50


Array of Pointers int a=1, b=2, c=3, d=4; int *k[4] = {&a, &b, &c, &d}; printf("%d %d %d %d", *k[0], *k[1],*k[2],*k[3]);
Описание слайда:
Array of Pointers int a=1, b=2, c=3, d=4; int *k[4] = {&a, &b, &c, &d}; printf("%d %d %d %d", *k[0], *k[1],*k[2],*k[3]);

Слайд 51


Strings In C, strings are just an array of characters Terminated with ‘\0’ character Arrays for bounded-length strings Pointer for constant strings...
Описание слайда:
Strings In C, strings are just an array of characters Terminated with ‘\0’ character Arrays for bounded-length strings Pointer for constant strings (or unknown length)

Слайд 52


Strings & Pointers
Описание слайда:
Strings & Pointers

Слайд 53


Strings in C (cont’d)
Описание слайда:
Strings in C (cont’d)

Слайд 54


char Array vs. char *: Example
Описание слайда:
char Array vs. char *: Example

Слайд 55


An Example char *str, s[] = "ALIREZA"; printf("%s", s); // ALIREZA printf(s); // ALIREZA printf("%s", s + 3); // REZA...
Описание слайда:
An Example char *str, s[] = "ALIREZA"; printf("%s", s); // ALIREZA printf(s); // ALIREZA printf("%s", s + 3); // REZA scanf("%s", s); scanf("%s", &s[0]); str = s; while(* str) putchar(*str++); // *s++ : Error

Слайд 56


Array of Pointers char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
Описание слайда:
Array of Pointers char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

Слайд 57


Empty vs. Null Empty string "" Is not null pointer Is not uninitialized pointer
Описание слайда:
Empty vs. Null Empty string "" Is not null pointer Is not uninitialized pointer

Слайд 58


Multi-Dimensional Arrays int a[row][col]; a[row][col]  *(a[row] + col) a  a[0][0]  a[0]
Описание слайда:
Multi-Dimensional Arrays int a[row][col]; a[row][col]  *(a[row] + col) a  a[0][0]  a[0]

Слайд 59


Call by value
Описание слайда:
Call by value

Слайд 60


Call by reference
Описание слайда:
Call by reference

Слайд 61


Pointers in Functions
Описание слайда:
Pointers in Functions

Слайд 62


Swap function (wrong version)
Описание слайда:
Swap function (wrong version)

Слайд 63


swap function (the correct version)
Описание слайда:
swap function (the correct version)

Слайд 64


Now we can get more than one value from a function Write a function to compute the roots of quadratic equation ax^2+bx+c=0. How to return two roots?...
Описание слайда:
Now we can get more than one value from a function Write a function to compute the roots of quadratic equation ax^2+bx+c=0. How to return two roots? void comproots(int a,int b,int c, double *dptr1, double *dptr2) { *dptr1 = (-b - sqrt(b*b-4*a*c))/(2.0*a); *dptr2 = (-b + sqrt(b*b-4*a*c))/(2.0*a); return; }

Слайд 65


Trace a program
Описание слайда:
Trace a program

Слайд 66


Pointer as the function output
Описание слайда:
Pointer as the function output

Слайд 67


Pointer as the function output
Описание слайда:
Pointer as the function output

Слайд 68


Pointer to constant: const *
Описание слайда:
Pointer to constant: const *

Слайд 69


Constant pointer: * const
Описание слайда:
Constant pointer: * const

Слайд 70


Passing Arrays to Functions #include void display(int a) { printf("%d",a); } int main() { int c[] = {2,3,4}; display(c[2]); //Passing array...
Описание слайда:
Passing Arrays to Functions #include void display(int a) { printf("%d",a); } int main() { int c[] = {2,3,4}; display(c[2]); //Passing array element c[2] only return 0; }

Слайд 71


Arrays in Functions
Описание слайда:
Arrays in Functions

Слайд 72


Passing Arrays to Functions #include float average(float a[], int count); // float average(float *a, int count) int main(){ float avg, c[]={23.4, 55,...
Описание слайда:
Passing Arrays to Functions #include float average(float a[], int count); // float average(float *a, int count) int main(){ float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18}; avg=average(c, 6); /* Only name of array is passed as argument */ printf("Average age=%.2f", avg); return 0; } float average(float a[], int count){ // float average(float *a int I; float avg, sum = 0.0; for(I = 0;I < count; ++i) sum += a[i]; avg = (sum / 6); return avg; }

Слайд 73


Passing Arrays to Functions #include void f1(float *a) { a[1] = 100;} void f2(float a[]){ a[2] = 200;} void printArray(float a[]) { int i = 0; for(;...
Описание слайда:
Passing Arrays to Functions #include void f1(float *a) { a[1] = 100;} void f2(float a[]){ a[2] = 200;} void printArray(float a[]) { int i = 0; for(; i < 6; i++) printf("%g ", a[i]); } int main(){ float c[]={23.4, 55, 22.6, 3, 40.5, 18}; f1(c); printArray(c); puts(""); f2(c); printArray(c); return 0; }

Слайд 74


Pointer to functions
Описание слайда:
Pointer to functions

Слайд 75


Pointer to Function #include void f1(float a){ printf("F1 %g", a);} void f2(float a){ printf("F2 %g", a);} int main(){ void...
Описание слайда:
Pointer to Function #include void f1(float a){ printf("F1 %g", a);} void f2(float a){ printf("F2 %g", a);} int main(){ void (*ptrF)(float a); ptrF = f1; ptrF(12.5); ptrF = f2; ptrF(12.5); getch(); return 0; }

Слайд 76


Example
Описание слайда:
Example

Слайд 77


Pointer to function
Описание слайда:
Pointer to function

Слайд 78


Pointers. Lecture18-20, слайд №78
Описание слайда:

Слайд 79


Pointers. Lecture18-20, слайд №79
Описание слайда:

Слайд 80


Dynamic Memory Allocation
Описание слайда:
Dynamic Memory Allocation

Слайд 81


Dynamic Memory Allocation Memory is allocated using the: malloc function (memory allocation) calloc function (cleared memory allocation) Memory is...
Описание слайда:
Dynamic Memory Allocation Memory is allocated using the: malloc function (memory allocation) calloc function (cleared memory allocation) Memory is released using the: free function note: memory allocated dynamically does not go away at the end of functions, you MUST explicitly free it up The size of memory requested by malloc or calloc can be changed using the: realloc function

Слайд 82


malloc Prototype: void *malloc(size_t size); function returns the address of the first byte programmers responsibility to not lose the pointer...
Описание слайда:
malloc Prototype: void *malloc(size_t size); function returns the address of the first byte programmers responsibility to not lose the pointer Example:

Слайд 83


calloc
Описание слайда:
calloc

Слайд 84


Example of malloc and calloc
Описание слайда:
Example of malloc and calloc

Слайд 85


Example
Описание слайда:
Example

Слайд 86


malloc and calloc
Описание слайда:
malloc and calloc

Слайд 87


malloc vs. calloc
Описание слайда:
malloc vs. calloc

Слайд 88


Free
Описание слайда:
Free

Слайд 89


free Prototype: void free(void *ptr) releases the area pointed to by ptr ptr must not be null trying to free the same area twice will generate an...
Описание слайда:
free Prototype: void free(void *ptr) releases the area pointed to by ptr ptr must not be null trying to free the same area twice will generate an error

Слайд 90


#include
Описание слайда:
#include

Слайд 91


#include
Описание слайда:
#include

Слайд 92


Reallocation
Описание слайда:
Reallocation

Слайд 93


realloc Example float *nums; int I; nums = (float *) calloc(5, sizeof(float)); /* nums is an array of 5 floating point values */ for (I = 0; I < 5;...
Описание слайда:
realloc Example float *nums; int I; nums = (float *) calloc(5, sizeof(float)); /* nums is an array of 5 floating point values */ for (I = 0; I < 5; I++) nums[I] = 2.0 * I; /* nums[0]=0.0, nums[1]=2.0, nums[2]=4.0, etc. */ nums = (float *) realloc(nums,10 * sizeof(float)); /* An array of 10 floating point values is allocated, the first 5 floats from the old nums are copied as the first 5 floats of the new nums, then the old nums is released */

Слайд 94


Pointers. Lecture18-20, слайд №94
Описание слайда:

Слайд 95


Allocating Memory for a Pointer There is another way to allocate memory so the pointer can point to something: #include #include int main(){ int *p;...
Описание слайда:
Allocating Memory for a Pointer There is another way to allocate memory so the pointer can point to something: #include #include int main(){ int *p; p = (int *) malloc( sizeof(int) ); /* Allocate 4 bytes */ scanf("%d", p); printf("%d", *p); // .... free(p); /* This returns the memory to the system*/ /* Important !!! */ }

Слайд 96


Allocating Memory for a Pointer You can use malloc and free to dynamically allocate and release the memory int *p; p = (int *) malloc(1000 *...
Описание слайда:
Allocating Memory for a Pointer You can use malloc and free to dynamically allocate and release the memory int *p; p = (int *) malloc(1000 * sizeof(int) ); for(i=0; i

Слайд 97


#include
Описание слайда:
#include

Слайд 98


Pointers. Lecture18-20, слайд №98
Описание слайда:

Слайд 99


Pointers. Lecture18-20, слайд №99
Описание слайда:

Слайд 100


Pointers. Lecture18-20, слайд №100
Описание слайда:

Слайд 101


Pointers. Lecture18-20, слайд №101
Описание слайда:

Слайд 102


Pointers. Lecture18-20, слайд №102
Описание слайда:



Теги Pointers Lecture18
Похожие презентации
Mypresentation.ru
Загрузить презентацию