a3: jetzt ist wirklich nur noch die letze Adresse falsch

This commit is contained in:
Johannes Theiner 2018-11-09 14:09:45 +01:00
parent 69ae656493
commit eebb2f998c
1 changed files with 8 additions and 19 deletions

View File

@ -10,9 +10,9 @@ void memdump(unsigned char *string) {
int rows = 4;
char array[columns * rows];
for(int i = 0; i < rows; i++) {
printf("0x%02x ", string[i]);
printf("%p ", (void *) &string[i * columns]);
for (int j = i * columns; j < (i + 1) * columns; j++) {
printf("%02x ", string[j]);
if(string[j] < 0x20 || string[j] > 0x7e) array[j] = '.';
@ -24,29 +24,19 @@ void memdump(unsigned char *string) {
printf("%c", array[j]);
}
printf("\n");
}
}
int memreplace(char *string, char cin, char cout, char **caddr) {
int found = 0;
char *bla = NULL;
for (int i = 0; i < strlen(string); ++i) {
if(string[i] == cin) {
found++;
//TODO: fix pointer assignment
//if(*caddr) free(caddr);
bla = &string[i];
string[i] = cout;
*caddr = &string[i];
}
*caddr = bla;
}
return found;
}
@ -69,14 +59,13 @@ int main(int argc, char **argv) {
printf("Ersetzen: '%c' mit '%c'\n", *argv[2], *argv[3]);
unsigned char *copy = malloc(4 * 16 * sizeof(char));
strcpy((char *) copy, argv[1]);
char **lastAddress = NULL;
int count = memreplace((char *) *copy, *argv[2], *argv[3], lastAddress);
char *lastAddress = NULL;
int count = memreplace((char *) copy, *argv[2], *argv[3], &lastAddress);
printf("Suchzeichen wurde %i mal gefunden und ersetzt\n", count);
//TODO: change type
printf("zuletzt an Addresse 0x%02x \n", (unsigned int) &lastAddress);
printf("zuletzt an Addresse %p \n", *lastAddress);
memdump(copy);
free(copy);