User Tools

Site Tools


isc:cv4codes

Kódy

* Různé části kódů ukázané na cviku

array.c
#include <stdio.h>
/*
        +------+------+------+------+
 n[4] = | n[0] | n[1] | n[2] | n[3] |
        +------+------+------+------+
*/
 
 
int main() {
 
    //int n[10];     // random hodnoty v pameti, ale prostor mam alokovany, takze je to v "poradku"
    //int n[10] = {0,0,0,0,0,0,0,0,0,0}; // definované hodnoty
    int n[] = {0,0,0,0,0,0,0,0,0,0};
 
    for(int i = 0; i < 10; i++) {
        printf("[%d]: %d\n", i, n[i]);
    }
 
    printf("Filing\n");
    for(int i = 0; i < 10; i++) {
        n[i] = i + 100;
    }
 
    for(int j = 0; j < 10; j++) {
        printf("n[%d] = %d\n", j, n[j]);
    }
 
    printf("= VELIKOSTI =\n");
    printf("sizeof(n): %zu\n", sizeof(n));
    printf("sizeof(n[0]): %zu\n", sizeof(n[0]));
    printf("sizeof(int): %zu\n", sizeof(int));
    printf("sizeof(n)/sizeof(n[0]): %zu\n", sizeof(n)/sizeof(n[0]));
    printf("sizeof(n)/sizeof(int): %zu\n", sizeof(n)/sizeof(int));
 
    return 0;
}

string.c
#include <stdio.h>
 
int main(int argc, char *argv[])
{
    char *str1 = "Abcde";               // retezcovy literal, nelze menit
    char str2[] = "Abcde";              // pole znaku, lze menit, '\0' na konci automaticky
    char str3[] = {'A','b','c','d','e','\0'};   // ekvivalentni k str2
 
    printf("str1 = %s\n", str1);
    printf("str2 = %s\n", str2);
    printf("str3 = %s\n", str3);
 
    str1[1] = 'X';     // zde to selze, SIGSEGV
    str2[1] = 'X';
    str3[1] = 'X';
 
    printf("str1 = %s\n", str1);
    printf("str2 = %s\n", str2);
    printf("str3 = %s\n", str3);
 
    // Vypsani retezce str2 po znacich
    for(int i = 0; str2[i] != '\0'; i++) {
        printf("%c", str2[i]);
    }
 
    // Vypsani argumentuu
    for(int i = 0; i < argc; i++) {
        printf("%d. argument = %s\n", i, argv[i]);
    }
 
    return 0;
}

pointers_as_argument.c
#include <stdio.h>
/// & - vraci misto v pameti, kde je hodnota skutecne ulozena
/// * - zpristupnuje misto v pameti
 
/*
int add(int x, int y) {
    return x + y;
} */
 
// funkce, ktera nic nevraci, ale i tak ziskam vysledek
void add(int x, int y, int *z) {
    (*z) = x + y;
    //printf("x = %d \ny: %d \nz: %d \n*z: %d\n", x, y, z, *z);    // pro info
}
 
int main() {
 
    int a = 4;
    int b = 2;
    int c = 0;
 
    //c = add(a, b);
    add(a, b, &c);
 
    printf("%d + %d = %d\n", a, b, c);
 
    return 0;
}

swap.c
#include <stdio.h>
 
// prohozeni dvou hodnot
void swap(int *a, int *b) {
    int tmp = *a;
    *a = *b;
    *b = tmp;
}
 
int main() {
 
    int a = 4;
    int b = 2;
 
    printf("a = %d, b = %d\n", a, b);
 
    swap(&a, &b);
 
    printf("a = %d, b = %d\n", a, b);
 
    return 0;
}

struct.c
#include <stdio.h>
 
struct SomeType {
    char *login;
    char *name;
    int age;
};
 
typedef struct SomeAddr {
    char *street;
    int house_number;
    char *city;
    int postal_code;
} address;    // novy datovy typ
 
 
int main() {
    struct SomeType student1;
    student1.name = "Marty Login";
    student1.login = "xlogin00";
    student1.age = 42;
    printf("Login: %s\n\n", student1.login);
 
    address recipient = {"Boulevard", 42, "Las Vegas", 89109};
    printf("Address of recipient:\n %s %d\n %s, %d",
        recipient.street, recipient.house_number, recipient.city, recipient.postal_code);
 
    return 0;
}
isc/cv4codes.txt · Last modified: 2024/10/09 17:10 by sakin