- Pointer
- Dynamischer Speicher
- sizeof
- Strukturen
- File I/O
- Pipes
- Gruppenbeispiel
<datentyp>* name;
*name = <value>;
name = &<variable>;
1int main()
{
2 int a = 3;
3 int x = 2;
4 int* ptr = &a;
5 *ptr = 4;
6 ptr = &x;
7 *ptr = 8;
return 0;
}
|
|
size
an (in Bytes angegeben)
1int main()
{
2 int* mem = (int*)malloc(sizeof(int) * 2);
3 if(mem == NULL) return 0;
4 *mem = 1;
5 *(mem + 1) = 2;
6 mem[0] = 3;
7 mem[1] = 4;
8 free(mem);
return 0;
}
|
|
4void getHM(int minutes, int* h, int* m)
{
5 *h = minutes / 60;
6 *m = minutes % 60;
}
1int main()
{
2 int hour = 0, minutes = 0;
3 getHourMinutes(320, &hour, &minutes);
7 printf("%d:%d\n", hour, minutes);
return 0;
}
|
|
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char* text = (char*)malloc(6 * sizeof(char)); strcpy(text, "Hallo!"); printf("%s\n", text); return 0; }Kompilieren:
gcc -Wall -g -o valgrind_test valgrind_test.cValgrind:
valgrind --leak-check=full ./valgrind_test
NULL
initialisierenfree
auf NULL
setzenmalloc
/realloc
auf NULL
überprüfenrealloc
Hilfspointer verwenden:
char* text = (char*)malloc(8 * sizeof(char)); char* new_text = (char*)realloc(text, 16 * sizeof(char)); if(new_text != NULL) { text = new_text; } else { free(text); printf("Out of memory!\n"); }
1int* x = (int*)malloc(3);
2x = (int*)realloc(x, 16);
3if(x == NULL)
{
4 free(x);
5 printf("Out of memory!");
}
|
|
1int* x = (int*)malloc(3);
2int* new_x = (int*)realloc(x, 16);
3if(new_x != NULL)
{
x = new_x;
}
4else
{
5 free(x);
6 printf("Out of memory!\n");
}
|
|
int charsize = sizeof(char);
int ptrsize = sizeof(int*);
int size = sizeof("Hallo");
int x[20]; int len = sizeof(x) / sizeof(x[0]);
int* x = (int*)malloc(100); int len = sizeof(x);
int x[20]; int* y = x; int len = sizeof(y) / sizeof(y[0]);
char* hallo = "Hallo"; int size = sizeof(hallo);
int* x = (int*)malloc(10);
int* x = (int*)malloc(10 * sizeof(int));
#include <stdio.h> struct _Person_ { char* name; int age; }; int main() { struct _Person_ petra; petra.name = "Petra"; petra.age = 17; return 0; }
typedef
:
[...] struct _Person_ { char* name; int age; }; typedef struct _Person_ Person; [...] Person petra; petra.name = "Petra"; petra.age = 17; [...]
[...] struct _Person_ { char* name; int age; }; typedef struct _Person_ Person; [...] Person* petra = (Person*)malloc(sizeof(Person)); petra->name = "Petra"; petra->age = 17; [...] free(petra);
struct _Person_ { char* name; int age; struct _Person_* father; struct _Person_* mother; };
fopen(const char * filename, const char * mode);
fread(void * dst, size_t size, size_t items, FILE * file);
fwrite(const void * data, size_t size, size_t items, FILE * file);
struct
in Datei speichernstruct
s können in Binärdateien gespeichert werden[...] struct _Phone_ { int country_code; int number; }__attribute__((packed)); [...] struct _Phone_ number; number.number = 1234567; number.country_code = 43; FILE* file = fopen("phone_numbers", "wb"); // wb = write binary fwrite(&number, sizeof(struct _Phone_), 1, file); fclose(file);
struct
aus Datei lesenstruct
s können aus Binärdateien geladen werden[...] struct _Phone_ { int country_code; int number; }__attribute__((packed)); [...] struct _Phone_ number; FILE* file = fopen("phone_numbers", "rb"); // rb = read binary fread(&number, sizeof(struct _Phone_), 1, file); fclose(file);
int
in einer Datei aus?78 56 34 12
char
Array eingelesen: 02 01 00 00
2560 | 2561 | 2562 | 2563 |
---|---|---|---|
02 | 01 | 00 | 00 |
fread
:
int length; fread(&length, sizeof(int), 1, file);
// buffer is the char array containing the file int length = *((int*)buffer);
memcpy
in einen Integer kopieren:
// buffer is the char array containing the file int length; memcpy(&length, buffer, sizeof(int));
long size; fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET);
Möglichkeit, die Ausgabe der einzelnen Befehle umzuleiten bzw. an andere Befehle weiterzuleiten.
./programm > ausgabe.txt
./programm < eingabe.txt
./programm 2> error.txt
./programm < input.txt 1> output.txt 2> error.txt
./programm | tail
echo $?
Abgabeschluss: