P
pater
I wrote this function by myself, but program crashes when this func
gets called... why?
------
void replace(char *data, char *what, char *with) {
int i, ldata = strlen(data), lwhat = strlen(what), lwith = strlen
(with);
char *buffer = (char *) malloc(ldata);
for(i=0;i<ldata-1;i++)
if(strncmp(&data, what, strlen(what)) == 0) {
if(lwhat<lwith) buffer = realloc(buffer, ldata+lwith-lwhat);
memcpy(&buffer, with, lwith);
i += lwith-1;
} else memcpy(&buffer, &data, 1);
memcpy(&buffer[ldata], 0, 1);
data = buffer;
}
--------
Some doubts then:
1) malloc returns a pointer, no? so, the "buffer" var should contain
the address of the heap address, shouldn't it?
2) If so, memcpy wants a pointer as 1st and 2nd argument.. why should
i pass &buffer instead of simply buffer?
I have still some doubts about pointers... and reference/dereference
operators..
gets called... why?
------
void replace(char *data, char *what, char *with) {
int i, ldata = strlen(data), lwhat = strlen(what), lwith = strlen
(with);
char *buffer = (char *) malloc(ldata);
for(i=0;i<ldata-1;i++)
if(strncmp(&data, what, strlen(what)) == 0) {
if(lwhat<lwith) buffer = realloc(buffer, ldata+lwith-lwhat);
memcpy(&buffer, with, lwith);
i += lwith-1;
} else memcpy(&buffer, &data, 1);
memcpy(&buffer[ldata], 0, 1);
data = buffer;
}
--------
Some doubts then:
1) malloc returns a pointer, no? so, the "buffer" var should contain
the address of the heap address, shouldn't it?
2) If so, memcpy wants a pointer as 1st and 2nd argument.. why should
i pass &buffer instead of simply buffer?
I have still some doubts about pointers... and reference/dereference
operators..