Programowanie Proceduralne Bożena Woźna-Szcześniak bwozna@gmail.com Jan Długosz University, Poland Wykład 6
Wskaźniki i tablice int a[10], b[10]; int* c; c = &a[0]; // c wskazuje na pierwszy element tablicy a c = a; // taki sam efekt jak poprzednia instrukcja b = a; // niepoprawne a = c;// niepoprawne Nazwa tablicy jest stałym wskaźnikiem do pierwszego elementu tej tablicy T tab[n]; // wyrażenie tab jest typu T* const
Wskaźniki i tablice int a[10]; Wyrażenia a[k] oraz *(a + k) sa równoważne. W szczególności równoważne sa wyrażenia: a[0] oraz *a
Wskaźniki i tablice int tab[10]; int* p = tab + 2; Jak przy pomocy tab napisać wyrażenia odpowiadające następujacym wyrażeniom z lewej kolumny tabeli? p tab + 2 *p tab[2] p[0] tab[2] p + 6 tab + 8 *p + 6 tab[2] + 6 *(p + 6) tab[8] &p p[-1] tab[1]
Wskaźniki i tablice int a[100], b[100]; int* p = a; int* q = a + 50; int* r = a + 100; int* s = b; W arytmetyce na wskaźnikach można posłużyć się adresem pierwszego miejsca po ostatnim elemencie tablicy Rozważmy wyrażenia p - q, q - p p < q, p <= q, p > q, p >= q, p == q, p!= q p - s, s - p, p < s,... NIEOKREŚLONE!!!
Typy stałe int const rozmiarbufora = 512; int const rozmiarbufora; // niepoprawne int const* p = &rozmiarbufora; int* p = &rozmiarbufora; // niepoprawne int a, b; p = &a; // poprawne a = 1024; *p = 2048; // niepoprawne int* const q = &a; q = &b; // niepoprawne int const* const r = &rozmiarbufora;
p07a kopiowanie tablicy przy pomocy wskaźników p07b kopiowanie tablicy przy pomocy wskaźników p08a obliczanie długości ciagu znaków p08b obliczanie długości ciagu znaków p09 funkcja biblioteczna strlen
p07a #include <stdio.h> #include <stdlib.h> void arraycopy(long* dest, long const* src, int n); void show(long const*, int); int main(void) long a[] = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31; int const n = sizeof(a) / sizeof(a[0]); long b[n]; show(a, n); arraycopy(b, a, n); show(b, n); return 0;
p07a - cd. void arraycopy(long* dest, long const* src, int n) while (n > 0) *dest = *src; ++dest; ++src; --n; void show(long const* p, int n) long const* q = p + n; while (p < q) printf("%ld ", *p); ++p; printf("\n");
p07b #include <stdio.h> #include <stdlib.h> void arraycopy(long* dest, long const* src, int n); void show(long const*, int); int main(void) long a[] = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31; int const n = sizeof(a) / sizeof(a[0]); long b[n]; show(a, n); arraycopy(b, a, n); show(b, n); return 0;
p07b - cd. void arraycopy(long* dest, long const* src, int n) while (n-- > 0) *dest++ = *src++; void show(long const* p, int n) long const* q = p + n; while (p < q) printf("%ld ", *p++); printf("\n");
p08a #include <stdio.h> #include <stdlib.h> int strlen(char const* s); int main(void) char* pmsg = "Linux is fun, Slackware more fun, so have fun!"; printf("%d : %s\n", strlen(pmsg), pmsg); char amsg[] = "Linux is fun, Slackware more fun, so have fun!"; printf("%d : %s\n", strlen(amsg), amsg); return 0; int strlen(char const* s) int n = 0; for (; *s!= \0 ; ++s) ++n; return n;
p08b #include <stdio.h> #include <stdlib.h> int strlen(char const* s); int main(void) char* pmsg = "Linux is fun, Slackware more fun, so have fun!"; printf("%d : %s\n", strlen(pmsg), pmsg); char amsg[] = "Linux is fun, Slackware more fun, so have fun!"; printf("%d : %s\n", strlen(amsg), amsg); return 0; int strlen(char const* s) char const* p = s; while (*p!= \0 ) ++p; return p - s;
p08b - niepoprawna. Dlaczego? #include <stdio.h> #include <stdlib.h> int strlen(char const* s); int main(void) char* pmsg = "Linux is fun, Slackware more fun, so have fun!"; printf("%d : %s\n", strlen(pmsg), pmsg); char amsg[] = "Linux is fun, Slackware more fun, so have fun!"; printf("%d : %s\n", strlen(amsg), amsg); return 0; int strlen(char const* s) char const* p = s; while (*p++!= \0 ) return p - s;
p09 #include<stdio.h> int main (void) char B[]="Agata noga zamiata"; puts(b); B[0]= a ; puts(b); char *C="Zima jest zimna"; puts(c); C[0]= z ; //Czy poprawne? return 0;