Презентация Pointers. Lecture18-20 онлайн
На нашем сайте вы можете скачать и просмотреть онлайн доклад-презентацию на тему Pointers. Lecture18-20 абсолютно бесплатно. Урок-презентация на эту тему содержит всего 102 слайда. Все материалы созданы в программе PowerPoint и имеют формат ppt или же pptx. Материалы и темы для презентаций взяты из открытых источников и загружены их авторами, за качество и достоверность информации в них администрация сайта не отвечает, все права принадлежат их создателям. Если вы нашли то, что искали, отблагодарите авторов - поделитесь ссылкой в социальных сетях, а наш сайт добавьте в закладки.
Презентации » Устройства и комплектующие » Pointers. Lecture18-20
Оцените!
Оцените презентацию от 1 до 5 баллов!
- Тип файла:ppt / pptx (powerpoint)
- Всего слайдов:102 слайда
- Для класса:1,2,3,4,5,6,7,8,9,10,11
- Размер файла:317.86 kB
- Просмотров:90
- Скачиваний:0
- Автор:неизвестен
Слайды и текст к этой презентации:
№5 слайд
![Recall Variables Recall a](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img4.jpg)
Содержание слайда: 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](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img5.jpg)
Содержание слайда: 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](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img6.jpg)
Содержание слайда: 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 ……
№15 слайд
![Using Pointers You can use](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img14.jpg)
Содержание слайда: 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
№21 слайд
![Pointer Fundamentals When a](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img20.jpg)
Содержание слайда: 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
№23 слайд
![Characteristics of Pointers](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img22.jpg)
Содержание слайда: 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](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img23.jpg)
Содержание слайда: 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.
№26 слайд
![Constant Pointers int x](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img25.jpg)
Содержание слайда: 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](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img26.jpg)
Содержание слайда: 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 */
№33 слайд
![NULL Pointer Often used as](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img32.jpg)
Содержание слайда: 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.
№36 слайд
![Arithmetic Operations When an](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img35.jpg)
Содержание слайда: 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
№38 слайд
![Arithmetic Operations A](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img37.jpg)
Содержание слайда: 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
№40 слайд
![Comparing Pointers Pointers](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img39.jpg)
Содержание слайда: 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](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img40.jpg)
Содержание слайда: 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](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img41.jpg)
Содержание слайда: 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)
№64 слайд
![Now we can get more than one](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img63.jpg)
Содержание слайда: 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;
}
№72 слайд
![Passing Arrays to Functions](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img71.jpg)
Содержание слайда: 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](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img72.jpg)
Содержание слайда: 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;
}
№81 слайд
![Dynamic Memory Allocation](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img80.jpg)
Содержание слайда: 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
№93 слайд
![realloc Example float nums](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img92.jpg)
Содержание слайда: 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 */
№95 слайд
![Allocating Memory for a](/documents_6/cac77a4978c322cbf9c5e3f0a9302bd1/img94.jpg)
Содержание слайда: 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 !!! */
}
Скачать все slide презентации Pointers. Lecture18-20 одним архивом:
Похожие презентации
-
Files. Lecture 25
-
Two-Level Logic Minimization Algorithms. Lecture 3
-
CSE 326: Data Structures. Mergeable Heaps. Lecture 22
-
Two pointers method
-
Указатели (pointers)
-
Analysis and Design of Data Systems. Complex SQL Queries (Lecture 13)
-
Analysis and Design of Data Systems. Functional Dependencies and Normalization Theory (Lecture 15)
-
Analysis and Design of Data Systems. Relational Algebra 2 (Lecture 18)
-
Data Types and Operators (Java, Lecture 04)
-
Generics. (Lecture 7. 3)