Add completely dynamic solution.
This commit is contained in:
parent
cd456d0712
commit
5169496e00
@ -58,6 +58,11 @@ int main()
|
||||
printf("\nNach Entfernen:\n");
|
||||
printNames();
|
||||
|
||||
clearArchive();
|
||||
printf("\nNach Clear-Aufruf:\n");
|
||||
printNames();
|
||||
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@ -5,14 +5,17 @@
|
||||
|
||||
#define INDEX_NOT_FOUND -1
|
||||
|
||||
static char *names[MAX_NAMES];
|
||||
static char **names = NULL;
|
||||
static unsigned int numberOfEntries = 0;
|
||||
|
||||
int addName(const char *name)
|
||||
{
|
||||
if(numberOfEntries < MAX_NAMES)
|
||||
char **oldPtr = names;
|
||||
names = realloc(names, sizeof(char*)*(numberOfEntries+1));
|
||||
|
||||
if(names != NULL)
|
||||
{
|
||||
names[numberOfEntries] = (char *)malloc(strlen(name)+1);
|
||||
names[numberOfEntries] = malloc((strlen(name)+1) * sizeof(sizeof(char)));
|
||||
|
||||
if(names[numberOfEntries] != NULL)
|
||||
{
|
||||
@ -22,6 +25,8 @@ int addName(const char *name)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
names = oldPtr;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -52,6 +57,7 @@ static void removeAt(int idx)
|
||||
names[i] = names[i+1];
|
||||
}
|
||||
numberOfEntries--;
|
||||
names = realloc(names, numberOfEntries*sizeof(char*));
|
||||
}
|
||||
|
||||
int removeName(const char *name)
|
||||
@ -68,3 +74,15 @@ int removeName(const char *name)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void clearArchive()
|
||||
{
|
||||
for(int i = 0; i < numberOfEntries; i++)
|
||||
{
|
||||
free(names[i]);
|
||||
names[i] = NULL;
|
||||
}
|
||||
free(names);
|
||||
names = NULL;
|
||||
numberOfEntries = 0;
|
||||
}
|
||||
@ -1,12 +1,6 @@
|
||||
#ifndef NAMEARCHIVE_H
|
||||
#define NAMEARCHIVE_H
|
||||
|
||||
// Maximale Namenslänge
|
||||
#define MAX_NAME_LEN 10
|
||||
|
||||
// Maximale Anzahl an Namen
|
||||
#define MAX_NAMES 100
|
||||
|
||||
// Fügt einen Namen hinzu. Im Fehlerfall (Archiv ist voll) soll 0, ansonsten 1 zurückgegeben werden.
|
||||
// Ist der angegebene Name länger als die zulässige Namenslänge, wird der Name abgeschnitten hinzugefügt. Rückgabe ist dann 1.
|
||||
int addName(const char *name);
|
||||
@ -18,5 +12,7 @@ int removeName(const char *name);
|
||||
void sortNames();
|
||||
// Gibt die Namen zeilenweise aus.
|
||||
void printNames();
|
||||
// Leere Archiv
|
||||
void clearArchive();
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user