Add pointer array solution.
This commit is contained in:
1 parent
68c0349f04
commit
5de350b565
3 files changed
+30
-16
No files matched your search
@@ -58,6 +58,8 @@ int main()
|
|||||||
printf("\nNach Entfernen:\n");
|
printf("\nNach Entfernen:\n");
|
||||||
printNames();
|
printNames();
|
||||||
|
|
||||||
|
clearArchive();
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+25
-13
@@ -3,7 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "namesarchive.h"
|
#include "namesarchive.h"
|
||||||
|
|
||||||
static char names[MAX_NAMES][MAX_NAME_LEN];
|
static char *names[MAX_NAMES];
|
||||||
static unsigned int numberOfNames = 0;
|
static unsigned int numberOfNames = 0;
|
||||||
|
|
||||||
static int findNameIdx(const char *name);
|
static int findNameIdx(const char *name);
|
||||||
@@ -14,15 +14,18 @@ static int compareNames(const void *arg1, const void *arg2);
|
|||||||
// Ist der angegebene Name länger als die zulässige Namenslänge, wird der Name abgeschnitten hinzugefügt. Rückgabe ist dann 1.
|
// 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)
|
int addName(const char *name)
|
||||||
{
|
{
|
||||||
if(numberOfNames < MAX_NAMES)
|
if(name != NULL && numberOfNames < MAX_NAMES)
|
||||||
{
|
{
|
||||||
strncpy(names[numberOfNames], name, MAX_NAME_LEN);
|
names[numberOfNames] = malloc(sizeof(*name) * (strlen(name) + 1));
|
||||||
names[numberOfNames][MAX_NAME_LEN-1] = '\0';
|
|
||||||
numberOfNames++;
|
if(names[numberOfNames] != NULL)
|
||||||
return 1;
|
{
|
||||||
|
strncpy(names[numberOfNames], name, strlen(name)+1);
|
||||||
|
numberOfNames++;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wie addName. Fügt Namen aber direkt sortiert hinzu. Voraussetzung ist ein bereits sortiertes Archiv.
|
// Wie addName. Fügt Namen aber direkt sortiert hinzu. Voraussetzung ist ein bereits sortiertes Archiv.
|
||||||
@@ -59,20 +62,20 @@ int findNameIdx(const char *name)
|
|||||||
|
|
||||||
void removeNameByIdx(unsigned int idx)
|
void removeNameByIdx(unsigned int idx)
|
||||||
{
|
{
|
||||||
|
free(names[idx]);
|
||||||
for(unsigned i = idx+1; i < numberOfNames; i++)
|
for(unsigned i = idx+1; i < numberOfNames; i++)
|
||||||
{
|
{
|
||||||
strncpy(names[i-1], names[i], MAX_NAME_LEN);
|
names[i-1] = names[i];
|
||||||
names[i-1][MAX_NAME_LEN-1] = '\0';
|
|
||||||
}
|
}
|
||||||
numberOfNames--;
|
numberOfNames--;
|
||||||
}
|
}
|
||||||
|
|
||||||
int compareNames(const void *arg1, const void *arg2)
|
int compareNames(const void *arg1, const void *arg2)
|
||||||
{
|
{
|
||||||
const char (*name1)[MAX_NAME_LEN] = arg1;
|
const char *name1 = *((const char **)arg1);
|
||||||
const char (*name2)[MAX_NAME_LEN] = arg2;
|
const char *name2 = *((const char **)arg2);
|
||||||
|
|
||||||
return strcmp(*name1, *name2);
|
return strcmp(name1, name2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sortiert die Namen im Archiv aufsteigend.
|
// Sortiert die Namen im Archiv aufsteigend.
|
||||||
@@ -89,3 +92,12 @@ void printNames()
|
|||||||
printf("%s\n", names[i]);
|
printf("%s\n", names[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void clearArchive()
|
||||||
|
{
|
||||||
|
for(int i = 0; i < numberOfNames; i++)
|
||||||
|
{
|
||||||
|
free(names[i]);
|
||||||
|
}
|
||||||
|
numberOfNames = 0;
|
||||||
|
}
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
#ifndef NAMEARCHIVE_H
|
#ifndef NAMEARCHIVE_H
|
||||||
#define NAMEARCHIVE_H
|
#define NAMEARCHIVE_H
|
||||||
|
|
||||||
// Maximale Namenslänge
|
|
||||||
#define MAX_NAME_LEN 10
|
|
||||||
|
|
||||||
// Maximale Anzahl an Namen
|
// Maximale Anzahl an Namen
|
||||||
#define MAX_NAMES 100
|
#define MAX_NAMES 100
|
||||||
|
|
||||||
@@ -19,4 +16,7 @@ void sortNames();
|
|||||||
// Gibt die Namen zeilenweise aus.
|
// Gibt die Namen zeilenweise aus.
|
||||||
void printNames();
|
void printNames();
|
||||||
|
|
||||||
|
// Gibt Resourcen frei
|
||||||
|
void clearArchive();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user