Презентация Pointers. Lecture18-20 онлайн

На нашем сайте вы можете скачать и просмотреть онлайн доклад-презентацию на тему Pointers. Lecture18-20 абсолютно бесплатно. Урок-презентация на эту тему содержит всего 102 слайда. Все материалы созданы в программе PowerPoint и имеют формат ppt или же pptx. Материалы и темы для презентаций взяты из открытых источников и загружены их авторами, за качество и достоверность информации в них администрация сайта не отвечает, все права принадлежат их создателям. Если вы нашли то, что искали, отблагодарите авторов - поделитесь ссылкой в социальных сетях, а наш сайт добавьте в закладки.



Оцените!
Оцените презентацию от 1 до 5 баллов!
  • Тип файла:
    ppt / pptx (powerpoint)
  • Всего слайдов:
    102 слайда
  • Для класса:
    1,2,3,4,5,6,7,8,9,10,11
  • Размер файла:
    317.86 kB
  • Просмотров:
    90
  • Скачиваний:
    0
  • Автор:
    неизвестен



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

№1 слайд
Lecture - Pointers
Содержание слайда: Lecture 18-20 Pointers

№2 слайд
Outline Defining and using
Содержание слайда: Outline Defining and using Pointers Operations on pointers Arithmetic Logical Pointers and Arrays Memory Management for Pointers

№3 слайд
Pointer Fundamentals 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: Pointer: Declaration and Initialization

№11 слайд
Addresses and Pointers int a,
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: * 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
Содержание слайда: 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 , n , p, q p
Содержание слайда: 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 int p p amp
Содержание слайда: 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 , b
Содержание слайда: 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
Содержание слайда: Value of referred memory by a pointer

№20 слайд
Exercise Trace the following
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: Allocating Memory for a Pointer

№23 слайд
Characteristics of Pointers
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: NULL Pointer Special constant pointer NULL Points to no data Dereferencing illegal To define, include <stdio.h> int *q = NULL;

№32 слайд
NULL Pointer
Содержание слайда: NULL Pointer

№33 слайд
NULL Pointer Often used as
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: Arithmetic Operations pointer + number pointer – number

№40 слайд
Comparing Pointers Pointers
Содержание слайда: Comparing Pointers Pointers can also be compared using ==, !=, <, >, <=, and >= 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 < unless both p and q “point” to variables in the same array.

№41 слайд
Logical Operations Pointers
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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 , i int p a int
Содержание слайда: 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 , p, q p amp
Содержание слайда: 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 , p a Error
Содержание слайда: 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 , b ,
Содержание слайда: 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
Содержание слайда: 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 amp 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 quot
Содержание слайда: 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
Содержание слайда: Array of Pointers char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

№57 слайд
Empty vs. Null Empty string
Содержание слайда: Empty vs. Null Empty string "" Is not null pointer Is not uninitialized pointer

№58 слайд
Multi-Dimensional Arrays int
Содержание слайда:  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
Содержание слайда: swap function (the correct version)

№64 слайд
Now we can get more than one
Содержание слайда: 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 lt
Содержание слайда: Pointer to constant: const <type> *

№69 слайд
Constant pointer lt type gt
Содержание слайда: Constant pointer: <type> * const

№70 слайд
Passing Arrays to Functions
Содержание слайда: Passing Arrays to Functions #include <stdio.h> 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
Содержание слайда: Passing Arrays to Functions #include <stdio.h> 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
Содержание слайда: Passing Arrays to Functions #include <stdio.h> 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
Содержание слайда: Pointer to Function #include <stdio.h> 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 слайд
Содержание слайда:

№79 слайд
Содержание слайда:

№80 слайд
Dynamic Memory Allocation
Содержание слайда: Dynamic Memory Allocation

№81 слайд
Dynamic Memory Allocation
Содержание слайда: 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
Содержание слайда: 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
Содержание слайда: 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 lt stdio.h gt
Содержание слайда: #include <stdio.h>

№91 слайд
include lt stdio.h gt
Содержание слайда: #include <stdio.h>

№92 слайд
Reallocation
Содержание слайда: Reallocation

№93 слайд
realloc Example float nums
Содержание слайда: 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 слайд
Содержание слайда:

№95 слайд
Allocating Memory for a
Содержание слайда: Allocating Memory for a Pointer There is another way to allocate memory so the pointer can point to something: #include <stdio.h> #include <stdlib.h> 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
Содержание слайда: 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<1000; i++) p[i] = i; p[999]=3; free(p); p[0]=5; /* Error! */

№97 слайд
include lt stdio.h gt
Содержание слайда: #include <stdio.h>

№98 слайд
Содержание слайда:

№99 слайд
Содержание слайда:

№100 слайд
Содержание слайда:

№101 слайд
Содержание слайда:

№102 слайд
Содержание слайда:

Скачать все slide презентации Pointers. Lecture18-20 одним архивом: