Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

11a. Příklady v C

#include <stdio.h>
#include <stdlib.h>
 
void tisk_pole (int * a, int velikost);
 
int main ()
{
	int a[2]; // neinicializovane pole
 
	int b[9] = {0};
 
	int c[] = {1, 2, 3, 4, 5};
 
	int d[] = {[100] = 10};
 
	int e[10] = {[4] = 10, [9] = 33};
 
	for (int i = 0; i < sizeof(e)/sizeof(int); i++)
		printf("%i ", e[i]);
	printf("\n");
 
	// ukazatel
	int x = 10, x1 = 20;
	printf("%p %p\n", &x, &x1);
	int * y = &x; // ukazatel y obsahuje adresu promenne x
	*(y+1) = 12;	// do pameti, jejiz adresu mam v y, zapisu 12
	printf("x = %i\n", x);
	printf("x1 = %i\n", x1);
 
	for (int i = 0; i < sizeof(e)/sizeof(int); i++)
		printf("%i ", *(e+1));
	printf("\n");
 
	int x2 = 20;
	int f[x2];
 
	int * g = malloc(4*sizeof(int));
	int * h = calloc(4, sizeof(int));	// nuluje odkazovanou pamet
 
	h = realloc (h, 5*sizeof(int));
 
	tisk_pole (c, 5);
 
	printf("%lu %lu\n", sizeof(g), sizeof(h));
 
	free(g);
	free(h);
 
	return 0;
}
 
void tisk_pole (int * a, int velikost)
{
	for (int i = 0; i < velikost; i++)
		printf("%i ", a[i]);
	printf("\n");
}

#include <stdio.h>
 
struct osoba
{
	int vyska;	// vyska v cm
	float vaha;	// vaha v kg
};
 
typedef struct 
{
	float re, im;
} komplex;
 
void komplex_tisk (komplex a)
{
	printf("%.2f%+.2fi\n", a.re, a.im); 
}
 
komplex komplex_soucet (komplex a, komplex b)
{
	komplex tmp;
	tmp.re = a.re + b.re; tmp.im = a.im + b.im;
	return tmp;
}
 
void tisk_osoby (struct osoba x)
{
	printf ("josef: %icm, %.2fkg\n", x.vyska, x.vaha);
}
 
int main()
{
	struct osoba josef;
 
	josef.vyska = 180; josef.vaha = 78.5; tisk_osoby (josef);
 
	komplex x; x.re = 3.14; x.im = 3.5;
	komplex x1; x1.re = -6.0; x1.im = 8.3;
	komplex_tisk (komplex_soucet (x, x1));
 
	typedef int i;
 
	i a = 10;
	printf("a = %i\n", a);
 
	return 0;
 
}

courses/bab37zpr/solutions/lab11a.txt · Last modified: 2019/12/16 09:30 by viteks