Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Программирование на ЯВУ, семинар 3
Search
Ilya
September 15, 2015
Education
0
130
Программирование на ЯВУ, семинар 3
Указатели, динамическая память, файлы.
Ilya
September 15, 2015
Tweet
Share
More Decks by Ilya
See All by Ilya
Программирование на ЯВУ, семинар 2
ilya
0
68
ОПК, семинар №8
ilya
0
49
ОПК, семинар №6
ilya
0
72
ОПК, семинар №4
ilya
0
100
ОПК, семинар №2
ilya
0
74
Other Decks in Education
See All in Education
情報Iの「縦糸」と「横糸」を意識したプログラム教育の実践
asial_edu
0
210
わたしのメタ学習 / My Own Meta Learning #shinjukurb
expajp
0
150
経験に複利を効かせろ!ふりかえり研修2024
pokotyamu
22
7.8k
2024年度春学期 統計学 第3回 クロス集計と感度・特異度/データの可視化 (2024. 4. 25)
akiraasano
PRO
0
100
外国人が思う日本語のむずかしいところ
jamashita
0
510
6ヶ月間の授業でここまでできた。コンテスト参加に向けての授業内容と生徒の様子を紹介!
asial_corp
0
410
Multimodal Interaction - Lecture 3 - Next Generation User Interfaces (4018166FNR)
signer
PRO
0
1.1k
子どもたち創造的活動機会の必要性に関する提言/creativehub
codeforeveryone
0
260
5 занятие. Разбор метода "8 кубиков"бизнес-модели #ideaNN 16.02.2024.
karlov
0
180
WordPressを教える人のための視点と考え方
crebowinfo
0
240
執筆テーマの決め方
sapi_kawahara
1
180
生成AIを活用できる大学教職員になる-基本と実践-
gmoriki
0
300
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
12
2.4k
A Modern Web Designer's Workflow
chriscoyier
689
190k
Making Projects Easy
brettharned
109
5.5k
How GitHub Uses GitHub to Build GitHub
holman
468
290k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
6
3.4k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
How to Ace a Technical Interview
jacobian
273
22k
We Have a Design System, Now What?
morganepeng
44
6.8k
The Art of Programming - Codeland 2020
erikaheidi
43
12k
The Pragmatic Product Professional
lauravandoore
26
5.8k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
11
1k
How STYLIGHT went responsive
nonsquared
92
4.8k
Transcript
Указатели, начало void increment(int x) { x++; printf("Increment: %d\n", x);
} void main() { int newx = 5; increment(newx); printf("Result: %d\n", newx); } > Increment: 6 > Result: 5
#define SIZE 3 void shuffle(double array[], unsigned size) { //
sizeof(array) == 8 ! } void main() { unsigned i; double array[SIZE] = {1.0, 2.0, 3.0}; shuffle(array, SIZE); for(i = 0; i < SIZE; i++) { printf("%.0lf ", array[i]); } } > 3 1 2
void main() { int newx = 5; scanf("%d", &newx); printf("Result:
%d\n", newx); } < 3 > Result: 3
Указатель — число, адрес в памяти. Диапазон значений зависит от
архитектуры процессора. Специальное значение — NULL.
void main() { int x = 5; int *px; px
= &x; // 0x7fff2a4defd4 printf("%d %d; %p\n", x, *px, px); (*px)++; printf("%d %d; %p\n", x, *px, px); } > 5 5; 0x7ffff8626ad4 > 6 6; 0x7ffff8626ad4 Работа с указателями
int newx; printf("%p", &newx); // 0x7fffc6c02cc4 printf("%p", &newy); // 0x7fffc6c02cc8
printf("%p", array); // 0x7fffc6c02ccc // 0x7fffc6c02cd0 // 0x7fffc6c02cd4 int newy; int array[3];
int newx; printf("%p", &newx); // 0x7fffc6c02cc4 printf("%p", &newy); // 0x7fffc6c02cc8
printf("%p", array); // 0x7fffc6c02ccc // 0x7fffc6c02cd0 // 0x7fffc6c02cd4 int newy; int array[3]; int *pointer = array;
int newx; printf("%p", &newx); // 0x7fffc6c02cc4 printf("%p", &newy); // 0x7fffc6c02cc8
printf("%p", array); // 0x7fffc6c02ccc // 0x7fffc6c02cd0 // 0x7fffc6c02cd4 int newy; int array[3]; int *pointer = array; pointer++;
void main() { int x = 5; int *px; px
= &x; // 0x7fff2a4defd4 printf("%d %d; %p\n", x, *px, px); (*px)++; printf("%d %d; %p\n", x, *px, px); *px++; printf("%d %d; %p\n", x, *px, px); } > 5 5; 0x7ffff8626ad4 > 6 6; 0x7ffff8626ad4 > 6 -127767848; 0x7ffff8626ad8 Подводный камень №1
Подводный камень №2 void func() { int x = 5;
// объявление указателя + присваивание int *px = &x; *px = &x; // не работает, тип *px - int px = &x; // присваивание }
Коды возврата void make_fibonacci(int n, int output[]) { if (n
< 0) // ? Как объяснить, что не так? }
// Возвращает 1 в случае успеха, 0 иначе int make_fibonacci(int
n, int output[]) { if (n < 0) { return 0; } // … return 1; } void main() { … if (make_fibonacci(n, output) != 0) { // Счастье, радость } }
Указатели и коды возврата: веселее вместе size_t fread( void *buffer,
size_t size, size_t count, FILE *stream ); fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count.
Массивы и указатели int array[4]; array[0], *(array + 0) array[2],
*(array + 2) int int int int int int int int int int int int
Массивы и указатели int array[4]; 0[array] 2[array] int int int
int int int int int int int int int
Массивы против указателей void shuffle(int *px, size_t n) { //
... } void shuffle(int px[], size_t n) { // ... }
Пустота и преобразование void main() { void *pointer; int x
= 5; pointer = &x; int *px = (int *) pointer; printf("%d\n", *px); }
Подводный камень №3 void main() { void *pointer; int x
= 5; pointer = &x; float *px = (float *) pointer; printf("%d\n", *px); } > -1679649000
Подводный камень №4 void main() { float f = 1.0;
char c = 'a'; printf("%f\n", (char) f); } > 0.000000
Динамическая память #include <malloc.h> void *malloc(size_t size); void free(void *memblock);
void *realloc(void *memblock, size_t size); void *calloc(size_t num, size_t size); void *memset(void *dest, int c, size_t count);
#include <malloc.h> #include <stdio.h> void main() { int *array =
NULL; int i; unsigned size; if(scanf("%u", &size) == 0) { return; } array = (int *) malloc(size * sizeof(int)); if(array == NULL) { return; } for(i = 0; i < size; i++) { array[i] = i; } free(array); }
array = (int *) calloc(size, sizeof(int)); for(i = 0; i
< size; i++) { printf("%d ", array[i]); } < 4 > 0 0 0 0
Файлы FILE *fopen( сonst char *filename, const char *mode); //
mode = r, w+, ab size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream); int fclose(FILE *stream);
#include <malloc.h> #include <string.h> #include <stdio.h> void main() { FILE*
file; size_t written; file = fopen("test.txt", "w"); if (file == NULL) { return; } char msg[] = "Hello, World!"; written = fwrite(msg, sizeof(char), strlen(msg), file); if (written == 0) { printf("Nothing written :(\n"); } fclose(file); }
int msg[] = {1, 2, 3, 4, 5}; written =
fwrite(msg, sizeof(int), 5, file); > cat test.txt > ^A^@^@^@^B^@^@^@^C^@^@^@^D^@^@^@^E^@^@^@
#define SIZE 5 void main() { FILE* file; size_t read;
int i; file = fopen("test.txt", "r"); if (file == NULL) { ... } int* message = (int*) malloc(SIZE * sizeof(int)); if (message == NULL) { ... fclose(file); } read = fread(message, sizeof(int), SIZE, file); if (read < SIZE) { printf("Read less than expected: %d\n", read); } for(i = 0; i < read; i++) { printf("%d ", message[i]); } free(message); fclose(file); }
int count, total = 0; char buffer[100]; FILE *stream; fopen_s(&stream,
"crt_feof.txt", "r"); if(stream == NULL) exit(1); while(!feof(stream)) { count = fread(buffer, sizeof(char), 100, stream); if(ferror(stream)) { perror("Read error"); break; } total += count; } printf("Number of bytes read = %d\n", total); fclose(stream );