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

@ -4,15 +4,15 @@
#include <memory.h> #include <memory.h>
void memdump(unsigned char *string) { void memdump(unsigned char *string) {
printf("ADDR 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\n"); printf("ADDR 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\n");
int columns = 16; int columns = 16;
int rows = 4; int rows = 4;
char array[columns * rows]; char array[columns * rows];
for(int i = 0; i < rows; i++) { 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++) { for (int j = i * columns; j < (i + 1) * columns; j++) {
printf("%02x ", string[j]); printf("%02x ", string[j]);
if(string[j] < 0x20 || string[j] > 0x7e) array[j] = '.'; if(string[j] < 0x20 || string[j] > 0x7e) array[j] = '.';
@ -24,29 +24,19 @@ void memdump(unsigned char *string) {
printf("%c", array[j]); printf("%c", array[j]);
} }
printf("\n"); printf("\n");
} }
} }
int memreplace(char *string, char cin, char cout, char **caddr) { int memreplace(char *string, char cin, char cout, char **caddr) {
int found = 0; int found = 0;
char *bla = NULL;
for (int i = 0; i < strlen(string); ++i) { for (int i = 0; i < strlen(string); ++i) {
if(string[i] == cin) { if(string[i] == cin) {
found++; found++;
string[i] = cout;
//TODO: fix pointer assignment *caddr = &string[i];
//if(*caddr) free(caddr);
bla = &string[i];
} }
*caddr = bla;
} }
return found; return found;
} }
@ -69,14 +59,13 @@ int main(int argc, char **argv) {
printf("Ersetzen: '%c' mit '%c'\n", *argv[2], *argv[3]); printf("Ersetzen: '%c' mit '%c'\n", *argv[2], *argv[3]);
unsigned char *copy = malloc(4 * 16 * sizeof(char)); unsigned char *copy = malloc(4 * 16 * sizeof(char));
strcpy((char *) copy, argv[1]); strcpy((char *) copy, argv[1]);
char **lastAddress = NULL; char *lastAddress = NULL;
int count = memreplace((char *) *copy, *argv[2], *argv[3], lastAddress); int count = memreplace((char *) copy, *argv[2], *argv[3], &lastAddress);
printf("Suchzeichen wurde %i mal gefunden und ersetzt\n", count); printf("Suchzeichen wurde %i mal gefunden und ersetzt\n", count);
//TODO: change type //TODO: change type
printf("zuletzt an Addresse 0x%02x \n", (unsigned int) &lastAddress); printf("zuletzt an Addresse %p \n", *lastAddress);
memdump(copy); memdump(copy);
free(copy); free(copy);