What is the most efficient way of doing this?
My first thought was to do:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *mybuf=(char *) malloc(1000);
mybuf="scribble here";
memset(mybuf, '\0', strlen(mybuf));
return 0;
}
But memset only takes a stack-based buffer/array, apparently.
Is it even possible to change the individual chars of a heap-based string?
I notice that while you can assign *string a new value. Like:
Code:
char *mybuf=(char *) malloc(1000);
mybuf="scribble here";
printf("%s\n\n", mybuf);
mybuf="write some more stuff";
printf("%s", mybuf);
you can't do something like:
Code:
char *mybuf=(char *) malloc(1000);
mybuf="scribble here";
int i;
for (i=0; i < strlen(mybuf); ++i){
mybuf[i]='a';
}
printf("%s", mybuf);
Why?