Syntax struct complex double real double image int
Syntax מבנים struct complex { double real; double image; }; int main() { struct complex c; struct complex* pc; c. real = 5; c. image = 7; pc = &c; pc->real = 3; pc->image = 4; } c 6024 real 3 5 image 4 7 pc 6024
Syntax מבנים struct complex { double real; double image; }; נשתמש בנקודה c של members לגישה ל -> נשתמש ב pc של members לגישה ל
double Abs. Complex(struct complex a) { return a. im * a. im + a. real * a. real; } struct complex Add(struct complex a, struct complex b) { struct complex s; s. im = a. im + b. im; s. real = a. real + b. real; return s; } שימוש בפונקציות x = Add(x, y); ! העברה לפי ערך ab = Abs. Complex(x);
typedef- מבנים ושימוש ב struct complex { double real; double image; }; int main() { struct complex c={5, 7}; struct complex* pc; } struct complex { double real; double image; }; typedef struct complex_t; int main() { complex_t c={5, 7}; complex_t* pc; } typedef struct complex_t; טיפוס קיים שם חדש לטיפוס קיים
double Abs. Complex(complex_t a) { return a. im * a. im + a. real * a. real; } complex_t Add(complex_t a, complex_t b) { complex_t s; s. im = a. im + b. im; s. real = a. real + b. real; return s; }
double Abs. Complex(complex_t* pa) { return pa->im * pa->im + pa->real * pa->real; } complex_t Add(complex_t* pa, complex_t* pb) { complex_t s; s. im = pa->im + pb->im; s. real = pa->real + pb->real; return s; }
פתרון int Max. Abs(complex_t *arr, int n( } int i, max. In; double max. Abs=-1; for (i=0; i<n; i(++ if (max. Abs < Abs. Complex(arr[i(([ } max. Abs = Abs. Complex(arr[i; ([ max. In = i; { return max. In; {
הקצאת זיכרון דינאמית #define SIZE 10 int main() { int *a, *b, i; a = (int*) malloc(sizeof(int) * SIZE); for (i=0; i<SIZE; i++) a[i] = i; } b = (int*) malloc(sizeof(int) *( SIZE + 1)) for (i=0; i<SIZE; i++) b[i] = a[i]; b[i] = i; free(a); free(b);
stdlib. h - הפונקציות נמצאות ב • void *malloc(size_t size); – Allocate size bytes. Successful call return base address; otherwise NULL is returned. • void *calloc(size_t n, size_t size_el) – Allocate an array on n elements each in size of size_el. Each bit is initialized to zero. Successful call return base address; otherwise NULL is returned. • void free(void *ptr) – Deallocate the memory pointed by ptr which must be a base address of previously allocated memory • void *realloc(void *, size_t size) – Reallocate a memory block of size.
דוגמא – הקצאת זיכרון דינאמית #include <stdio. h> #include <stdlib. h> void Fill. Array(int arr[], int n); void Print. Array(int arr[], int n); int main() { int *a, n; scanf("%d", &n); while( n< 0 || n > 1000) scanf("%d", &n); a = (int *)calloc(n, sizeof(int)); Fill. Array(a, n); Print. Array(a, n); free(a); return 0; } void Fill. Array(int arr[], int n) { int i; for(i = 0; i < n; i++) { arr[i] = rand()%100 + 1; } } void Print. Array(int arr[], int n) { int i; for(i = 1; i <= n; i++) { printf("%3 d ", arr[i - 1]); if (i % 10 == 0) printf("n"); } }
void main() { char *input=(char *)malloc(1), ch; int i, size=1, current=0; scanf("%c", &ch); while (ch != 'n') { if (current + 1 == size) { input = (char *)realloc(input, 2*size); size = size * 2; } input[current++] = ch; scanf("%c", &ch); } input[current]='